PHP Guestbook Lab

PHP Guestbook Lab

<p> PHP – GuestBook Lab</p><p>In this lab you will create a GuestBook page that stores and displays users who visit a website. </p><p>To complete the lab you must:  Create an HTML form called guestform.php  Send form input to PHP script guestform2.php  Store form input into a database table  Display database table in an HTML table </p><p>1. Create a new folder in your www folder called GuestLab. In this new folder, create the webpage guestform.php below that contains the following form: </p><p><html> <head><title>GuestBook</title></head> <body> <div id = "main"> <h1>GuestBook</h1> <form action="guestform2.php" method="post"> <table> <tr> <tr><td>Name</td><td><input type='text' name='guest_name' /></td></tr> <tr> <td>Email</td><td><input type='text' name='guest_email' /></td></tr> <tr><td><input type='submit' value='Submit' /> </td> </tr> </table> </form> </div> <div id="data"> </div> </body> </html></p><p>2. Open phpMyAdmin (http://localhost/phpmyadmin/). Create a new database called my_db . </p><p>3. Add a new table to the my_db database called Guests. The Guests table should contain 3 columns with the column names and data types below: name VARCHAR(50) email VARCHAR(50) submitted DATETIME </p><p>PHP – Guestbook Lab page 1 of 4 4. Create a new PHP script named guestform2.php. Save the new PHP form in the GuestLab folder you created in Step 1. This script will process the form data sent from guestform.php and insert a new record into the Guests table. Add the following code to guestform2.php : </p><p><?php </p><p>$con = mysql_connect("localhost","root",""); // connect to db if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); // select db </p><p>// get the name and email entered by user and store in variables $name = $_POST['guest_name']; $email = $_POST['guest_email']; </p><p>// create insert statement $sql = "insert into guests( name, email, submitted) values ('$name','$email', now() )"; mysql_query( $sql ); // insert record into table mysql_close($con); // close connection to db </p><p>// display thank you to user, and link back to form page echo "<h1>Thank you</h1>"; echo "<p>Thank you <b>$name</b> for your information.</p>"; echo "<p><a href='guestform.php'>Back to Guest Page</a></p>"; </p><p>?></p><p>5. Test the guest form by adding the following three entries: name email Joe Smith [email protected] Paul Wallace [email protected] Sam Byrd [email protected]</p><p>6. Go to localhost/phpmyadmin and Browse the contents of the Guests table to make sure the records were entered.</p><p>7. Let’s now revisit guestform.php and update the code to display a table of previously submitted visitors. Inside the division assigned the data id, add the following code:</p><p>PHP – Guestbook Lab page 2 of 4 <h2>Visitors</h2> <table> </p><p><?php // display table of entries from Guest table $con = mysql_connect("localhost","root",""); // connect to db if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); // select table </p><p>$result = mysql_query("SELECT * FROM Guests"); // create SELECT query </p><p>// for each record in table, display table row while($row = mysql_fetch_array($result)) { echo "<tr><td>" . $row['name'] . "</td><td>" . $row['email'] . "</td>"; echo "<td>" . $row['submitted'] . "</td></tr>"; } mysql_close($con); // close connection ?> </p><p></table></p><p>8. Download the guest_style.css page from the class website and save the css file in your GuestLab folder. Insert the <link> element in the <head>section to attach guest_style.css to guestform.php. </p><p>9. Next, update guestform2.php to automatically redirect the user back to guestform.php. In guestform2.php make the following change:</p><p>Replace the code: // display thank you to user, and link back to form page echo "<h1>Thank you</h1>"; echo "<p>Thank you <b>$name</b> for your information.</p>"; echo "<p><a href='guestform.php'>Back to Guest Page</a></p>";</p><p>With:</p><p>PHP – Guestbook Lab page 3 of 4 // redirect user back to guestform.php header("Location: guestform.php"); </p><p>10. Go back to the the form in guestform.php and add your name and your email into the form.</p><p>The new data you entered should automatically appear in the table on the page.</p><p>11. Once you have everything working, make the following changes to the Guest Form.</p><p>A. Add a new column to the Guests table named phone. Set the data type of phone to VARCHAR(20) B. Update the form in guestform.php to include a textbox named guest_phone for the visitor to enter their phone number. C. Update guestform2.php to get the phone number entered by the user that is stored in $_POST['guest_phone'] and store it in a variable D. Update the insert statement in guestform2.php to include the phone number E. Update guestform.php to include a new column for the phone number in the table of previously submitted visitors.</p><p>PHP – Guestbook Lab page 4 of 4</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us