SHARE
TWEET

Untitled

a guest Nov 8th, 2019 147 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8.     <title>Lab 6 Part 1</title>
  9.     <link href="https://fonts.googleapis.com/css?family=Staatliches&display=swap" rel=stylesheet>
  10.  
  11.     <style>
  12.         html {
  13.             text-align: center;
  14.         }
  15.  
  16.         img {
  17.             max-width: 500px;
  18.         }
  19.  
  20.     </style>
  21.  
  22. </head>
  23.  
  24. <body>
  25.  
  26.  
  27.  
  28.  
  29. <?php
  30. #connection setup
  31. $servername = "localhost";
  32. $username = "s2rajago";
  33. $password = "rhadyow*";
  34. $dbname = "s2rajago";
  35.  
  36. #create connection
  37. $conn = new mysqli($servername, $username, $password, $dbname);
  38.  
  39. #check connection
  40. if ($conn->connect_error) {
  41.     die("Connection failed: " . $conn->connect_error);
  42. }
  43. echo "Connected successfully <br><br>";
  44.  
  45. #create table
  46. $sql = "CREATE TABLE MyImages(
  47. id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  48. subject VARCHAR(30) NOT NULL,
  49. location VARCHAR(30) NOT NULL,
  50. date_taken VARCHAR(10) NOT NULL,
  51. url VARCHAR(500) NOT NULL
  52. )";
  53.  
  54. if (mysqli_query($conn, $sql)) {
  55.     echo "Table MyImages created successfully";
  56. } else {
  57.     echo "Error creating table: " . mysqli_error($conn);
  58. }
  59. echo "<br><br>";
  60.  
  61. #add to table
  62. $sql = "INSERT INTO MyImages (subject, location, date_taken, url)
  63. VALUES ('City of Toronto', 'Ontario', '2019-06-12', 'https://www.nationalgeographic.com/content/dam/travel/Guide-Pages/north-america/canada/Ontario/Ontario_h_00000218865162.adapt.1900.1.jpg')";
  64.  
  65. if ($conn->query($sql) === TRUE) {
  66.     $last_id = $conn->insert_id;
  67.     echo "New record created successfully. Last inserted ID is: " . $last_id;
  68. } else {
  69.     echo "Error: " . $sql . "<br>" . $conn->error;
  70. }
  71. echo "<br><br>";
  72.  
  73.  
  74. $sql = "INSERT INTO MyImages (subject, location, date_taken, url)
  75. VALUES ('Toronto Skyline', 'Ontario', '2018-02-11', 'https://www.journalpioneer.com/media/photologue/photos/cache/20601212_l_large.jpg')";
  76.  
  77. if ($conn->query($sql) === TRUE) {
  78.     $last_id = $conn->insert_id;
  79.     echo "New record created successfully. Last inserted ID is: " . $last_id;
  80. } else {
  81.     echo "Error: " . $sql . "<br>" . $conn->error;
  82. }
  83. echo "<br><br>";
  84.  
  85. $sql = "INSERT INTO MyImages (subject, location, date_taken, url)
  86. VALUES ('Parliment', 'Ontario', '2017-10-23', 'https://i.ytimg.com/vi/jTR2phKigFo/maxresdefault.jpg')";
  87.  
  88. if ($conn->query($sql) === TRUE) {
  89.     $last_id = $conn->insert_id;
  90.     echo "New record created successfully. Last inserted ID is: " . $last_id;
  91. } else {
  92.     echo "Error: " . $sql . "<br>" . $conn->error;
  93. }
  94. echo "<br><br>";
  95.  
  96. $sql = "INSERT INTO MyImages (subject, location, date_taken, url)
  97. VALUES ('P.E.I Lighthouse', 'Prince Edward Island', '2018-04-19', 'https://foodallergycanada.ca/wp-content/uploads/istock-482014132.jpg')";
  98.  
  99. if ($conn->query($sql) === TRUE) {
  100.     $last_id = $conn->insert_id;
  101.     echo "New record created successfully. Last inserted ID is: " . $last_id;
  102. } else {
  103.     echo "Error: " . $sql . "<br>" . $conn->error;
  104. }
  105. echo "<br><br>";
  106.  
  107.  
  108. #show the data sorted by date
  109. $sql = "SELECT id, subject, location, date_taken FROM MyImages ORDER BY date_taken";
  110.  
  111. $result = mysqli_query($conn, $sql);
  112.  
  113. if (mysqli_num_rows($result) > 0) {
  114.     // output data of each row
  115.     while($row = mysqli_fetch_assoc($result)) {
  116.         echo  "Id: " . $row["id"]. " - Subject: " . $row["subject"]. " - Location: " . $row["location"]. " - Date Taken: " . $row["date_taken"]. "<br>";
  117.     }
  118. } else {
  119.     echo "0 results";
  120. }
  121. echo "<br><br>";
  122.  
  123. #show the data with image and subject in ontario
  124. $sql = "SELECT subject, location, url FROM MyImages WHERE location = 'Ontario'";
  125.  
  126. $result = mysqli_query($conn, $sql);
  127.  
  128. if (mysqli_num_rows($result) > 0) {
  129.     // output data of each row
  130.     while($row = mysqli_fetch_assoc($result)) {
  131.         echo  "<img src=" . $row["url"]."><br>Subject: " . $row["subject"]. " - Location: " . $row["location"]. "<br>";
  132.     }
  133. } else {
  134.     echo "0 results";
  135. }
  136.  
  137.  
  138.  
  139. #Delete the whole table
  140. $sql = "DROP TABLE MyImages;";
  141.  
  142. if (mysqli_query($conn, $sql)) {
  143.     echo "Table deleted successfully";
  144. } else {
  145.     echo "Error deleting Table: " . mysqli_error($conn);
  146. }
  147.  
  148. $conn->close();
  149. ?>
  150.  
  151. </body>
  152.  
  153. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top