Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Lab 6 Part 1</title>
- <link href="https://fonts.googleapis.com/css?family=Staatliches&display=swap" rel=stylesheet>
- <style>
- html {
- text-align: center;
- }
- img {
- max-width: 500px;
- }
- </style>
- </head>
- <body>
- <?php
- #connection setup
- $servername = "localhost";
- $username = "s2rajago";
- $password = "rhadyow*";
- $dbname = "s2rajago";
- #create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- #check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- echo "Connected successfully <br><br>";
- #create table
- $sql = "CREATE TABLE MyImages(
- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- subject VARCHAR(30) NOT NULL,
- location VARCHAR(30) NOT NULL,
- date_taken VARCHAR(10) NOT NULL,
- url VARCHAR(500) NOT NULL
- )";
- if (mysqli_query($conn, $sql)) {
- echo "Table MyImages created successfully";
- } else {
- echo "Error creating table: " . mysqli_error($conn);
- }
- echo "<br><br>";
- #add to table
- $sql = "INSERT INTO MyImages (subject, location, date_taken, url)
- 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')";
- if ($conn->query($sql) === TRUE) {
- $last_id = $conn->insert_id;
- echo "New record created successfully. Last inserted ID is: " . $last_id;
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- echo "<br><br>";
- $sql = "INSERT INTO MyImages (subject, location, date_taken, url)
- VALUES ('Toronto Skyline', 'Ontario', '2018-02-11', 'https://www.journalpioneer.com/media/photologue/photos/cache/20601212_l_large.jpg')";
- if ($conn->query($sql) === TRUE) {
- $last_id = $conn->insert_id;
- echo "New record created successfully. Last inserted ID is: " . $last_id;
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- echo "<br><br>";
- $sql = "INSERT INTO MyImages (subject, location, date_taken, url)
- VALUES ('Parliment', 'Ontario', '2017-10-23', 'https://i.ytimg.com/vi/jTR2phKigFo/maxresdefault.jpg')";
- if ($conn->query($sql) === TRUE) {
- $last_id = $conn->insert_id;
- echo "New record created successfully. Last inserted ID is: " . $last_id;
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- echo "<br><br>";
- $sql = "INSERT INTO MyImages (subject, location, date_taken, url)
- VALUES ('P.E.I Lighthouse', 'Prince Edward Island', '2018-04-19', 'https://foodallergycanada.ca/wp-content/uploads/istock-482014132.jpg')";
- if ($conn->query($sql) === TRUE) {
- $last_id = $conn->insert_id;
- echo "New record created successfully. Last inserted ID is: " . $last_id;
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- echo "<br><br>";
- #show the data sorted by date
- $sql = "SELECT id, subject, location, date_taken FROM MyImages ORDER BY date_taken";
- $result = mysqli_query($conn, $sql);
- if (mysqli_num_rows($result) > 0) {
- // output data of each row
- while($row = mysqli_fetch_assoc($result)) {
- echo "Id: " . $row["id"]. " - Subject: " . $row["subject"]. " - Location: " . $row["location"]. " - Date Taken: " . $row["date_taken"]. "<br>";
- }
- } else {
- echo "0 results";
- }
- echo "<br><br>";
- #show the data with image and subject in ontario
- $sql = "SELECT subject, location, url FROM MyImages WHERE location = 'Ontario'";
- $result = mysqli_query($conn, $sql);
- if (mysqli_num_rows($result) > 0) {
- // output data of each row
- while($row = mysqli_fetch_assoc($result)) {
- echo "<img src=" . $row["url"]."><br>Subject: " . $row["subject"]. " - Location: " . $row["location"]. "<br>";
- }
- } else {
- echo "0 results";
- }
- #Delete the whole table
- $sql = "DROP TABLE MyImages;";
- if (mysqli_query($conn, $sql)) {
- echo "Table deleted successfully";
- } else {
- echo "Error deleting Table: " . mysqli_error($conn);
- }
- $conn->close();
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement