Upon completion of this workshop, you will be able to:
CREATE DATABASE vetoffice; |
CREATE TABLE owner(ownerid CHAR(2), fname VARCHAR(20),
lname VARCHAR(20), primary key(ownerid)); |
INSERT INTO owner VALUES("11","Laura","Reid"); |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Dr. Western's Vet Clinic</title> </head> <body> <h1>Welcome to the Western Vet Clinic</h1> <h2>Pets we look after</h2> <ol> <li>Dogs</li> <li>Cats</li> <li>Birds</li> </ol> </body> </html> |
<?php $dbhost = "localhost"; $dbuser= "root"; $dbpass = "cs3319"; $dbname = "vetoffice"; $connection = mysqli_connect($dbhost, $dbuser,$dbpass,$dbname); if (mysqli_connect_errno()) { die("database connection failed :" . mysqli_connect_error() . "(" . mysqli_connect_errno() . ")" ); } ?> |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Dr. Western's Vet Clinic</title> </head> <body> <?php include 'connectdbb.php'; ?> <h1>Welcome to the Western Vet Clinic</h1> <h2>Pets we look after</h2> <ol> <li>Dogs</li> <li>Cats</li> <li>Birds</li> </ol> </body> </html> |
<?php $query = "SELECT * FROM pet"; $result = mysqli_query($connection,$query); if (!$result) { die("databases query failed."); } while ($row = mysqli_fetch_assoc($result)) { var_dump($row); echo $row; } mysqli_free_result($result); ?> |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Dr. Western's Vet Clinic</title> </head> <body> <?php include 'connectdb.php'; ?> <h1>Welcome to the Western Vet Clinic</h1> <h2>Pets we look after</h2> <?php include 'getdata.php'; ?> <ol> <li>Dogs</li> <li>Cats</li> <li>Birds</li> </ol> </body> </html> |
<?php $query = "SELECT * FROM pet"; $result = mysqli_query($connection,$query); if (!$result) { die("databases query failed."); } echo "<ol>"; while ($row = mysqli_fetch_assoc($result)) { echo "<li>"; echo $row["species"] . "</li>"; } mysqli_free_result($result); echo "</ol>"; ?> |
<!DOCTYPE html> |
<?php $query = "SELECT * FROM owner"; $result = mysqli_query($connection,$query); if (!$result) { die("databases query failed."); } echo "Who are you looking up? </br>"; while ($row = mysqli_fetch_assoc($result)) { echo '<input type="radio" name="petowners" value="'; echo $row["ownerid"]; echo '">' . $row["fname"] . " " . $row["lname"] . "<br>"; } mysqli_free_result($result); ?> |
<!DOCTYPE html> |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Dr. Western's Vet Clinic</title> </head> <body> <?php include 'connectdb.php'; ?> <h1>Welcome to the Western Vet Clinic</h1> <h2>Pets we look after</h2> <form action="getpets.php" method="post"> <?php include 'getdata.php'; ?> <input type="submit" value="Get Pet Names"> </form> <p> <hr> <p> <h2> ADD A NEW PET:</h2> <form action="addnewpet.php" method="post"> New Pet's Name: <input type="text" name="petname"><br> New Pet's Species: <br> <input type="radio" name="species" value="dog">Dog<br> <input type="radio" name="species" value="cat">Cat<br> <input type="radio" name="species" value="bird">Bird<br> For which customer: <br> <?php include 'getdata.php'; ?> <input type="submit" value="Add New Pet"> </form> <?php mysqli_close($connection); ?> </body> </html> |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Dr. Western's Vet Clinic-Your Pets</title> </head> <body> <?php include 'connectdb.php'; ?> <h1>Here are your pets:</h1> <ol> <?php $whichOwner= $_POST["petowners"]; $petName = $_POST["petname"]; $species =$_POST["species"]; $query1= 'SELECT max(petid) AS maxid FROM pet'; $result=mysqli_query($connection,$query1); if (!$result) { die("database max query failed."); } $row=mysqli_fetch_assoc($result); $newkey = intval($row["maxid"]) + 1; $petid = (string)$newkey; $query = 'INSERT INTO pet values("' . $petid . '","' . $petName . '","' . $species . '","' . $whichOwner . '")'; if (!mysqli_query($connection, $query)) { die("Error: insert failed" . mysqli_error($connection)); } echo "Your pet was added!"; mysqli_close($connection); ?> </ol> </body> </html> |
... <h2> ADD A NEW PET:</h2> <form action="addnewpet.php" method="post" enctype="multipart/form-data" > New Pet's Name: <input type="text" name="petname"><br> New Pet's Species: <br> <input type="radio" name="species" value="dog">Dog<br> <input type="radio" name="species" value="cat">Cat<br> <input type="radio" name="species" value="bird">Bird<br> <input type="file" name="file" id="file"><br> For which customer: <br> <?php include 'getdata.php'; ?> <input type="submit" value="Add New Pet"> </form> ... |
<?php $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["file"]["name"]); $extension = end($temp); $extension = strtolower($extension); $uploadholder = dirname(__FILE__) . "/uploadarea"; if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 500000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { if (file_exists("uploadarea/" . $_FILES["file"]["name"])) { echo '<p><hr>'; echo $_FILES["file"]["name"] . " already exists. "; echo '<p><hr>'; $petpic = "NULL"; } else { move_uploaded_file($_FILES["file"]["tmp_name"],"uploadarea/" . $_FILES["file"]["name"]); $petpic = "uploadarea/" . $_FILES["file"]["name"]; } // end of else } // end of else } else { echo "Invalid file"; } //end of else ?> |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP cs3319 workshop</title> </head> <body> <h1>Some extra PHP stuff</h1> Here are some useful variables: <ul> <?php echo '<li>' . $_SERVER['SERVER_NAME'] ; echo '<li>' . $_SERVER['REMOTE_ADDR'] ; echo '<li>' . $_SERVER['DOCUMENT_ROOT'] ; echo '<li>' . $_SERVER['SCRIPT_FILENAME'] ; echo '<li>' . $_SERVER['PHP_SELF'] ; ?> </ul> <?php phpinfo(); ?> </body> </html> |
You have now created a little web application that shows the name of pet owners and their pets and adds new pets to an owner and displays the pet's pictures. Well done!
Make sure that you now go to BrightSpace>Assignments>Writing Some PHP Code and hand in a link to your Pet Application. You will get 1% for handing in this link. It should look like this: http://cs3319.gaul.csd.uwo.ca/vm???/phpworkshop/
where ??? is your virtual machine number. Remember to put a / at the very end of the URL or it might not work.