Hands-On Project 9-2

In this project, you create a Web page that allows visitors to your site to sign a guest book that is saved to a database.

1. Create a new document in your text editor and type the < !DOCTYPE> declaration, < html> element, document head, and < body> element. Use the strict DTD and "Guest Book" as the content of the < title> element.

2. Add the following text and elements to the document body:

< form>
< h2>Enter your name to sign our guest book
< form method="get" action="SignGuestBook.php">
< p>First Name < input type="text" name="first_name"/>< /p>
< p>Last Name < input type="text" name="last_name"/>< /p>
< p>< input type="submit" value="submit" />< /p>
< /form>

3. Save the document as GuestBook.html in the Projects directory for Chapter 9.

4. Create a new document in your text editor and type the < !DOCTYPE> declaration, < html> element, document head, and < body> element. Use the strict DTD and "Guest Book" as the content of the < title> element.

5. Add the following script section to the document body:

< ?php
?>

6. Add the following statements to the script section to ensure that visitors enter their first and last names:

if (empty($_GET['first_name']) || empty($_GET['last_name']))
die("< p>You must enter your first and last name! Click your brower's Back button to return to the Guest Book form.< /p>");

7. Add the following statement to the script section to connect to the database. Replace user and password with the MySQL username and password you created in Chapter 8.

$DBConnect = @mysqli_connect("localhost", "user", "password")
or die("< p>Unable to connect to the database server.< /p>"
. "< p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "< /p>";

8. Add the following statements to the end of the script section to create a database named

$DBName = "guestbook";
if (!@mysqli_select_db($DBConnect, $DBName)) {
$SQLstring = "CREATE DATABASE $DBName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
or die("< p>Unable to execute the query.< /p>"
. "< p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "< /p>";
echo "< p>You are the first visitor!< /p>";
mysqli_select_db($DBConnect, $DBName);
}

9.Add the following statements to the end of the script section to create a table named count if it does not already exist. The table consists of a single autoincrementing primary key field named countID.

$TableName = "visitors";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
if(!$QueryResult) {
$SQLstring = "CREATE TABLE $TableName (countID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
last_name VARCHAR(40), first_name VARCHAR(40))";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("< p>Unable to create the table.< /p>"
. "< p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "< /p>";
}

10.Finally, add the following statements to the end of the script section. These mysqli_query() statements add the visitor to the database and the last statement closes the database connection.

$LastName = addslashes($_GET['last_name']);
$FirstName = addslashes($_GET['first_name']);
$SQLstring = "INSERT INTO $TableName VALUES (NULL, '$LastName', '$FirstName')";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("< p>Unable to execute the query."< /p>"
. "< p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "< /p>";
echo "< h1>Thank you for signing our guest book!< /h1>";
mysqli_close($DBConnect);

11.Save the document as SignGuestBook.php in the Projects directory for Chapter 9.

12.Open GuestBook.html file in your Web browser by entering the following URL: http://localhost/PHP_Projects/Chapter.09/Projects/GuestBook.html.

Test the form to see if you can add your name to the database.

13.Close your Web browser window.

Hands-On Project 9-3

In this project, add a document to the Guest Book program you created in Hands-On Project 9-2. This document displays the entries in the guest book.

1.Create a new document in your text editor and type the < !DOCTYPE> declaration, < html> element, document head, and < body> element. Use strict DTD and "Guest Book" as the content of the < title> element.

2.Add the following script section to the document body:

< ?php
?>

3.Add the following statement to the script section to connect to the database. Replace user and password with the MySQL username and password you created in Chapter 8.

$DBConnect = @mysqli_connect("localhost", "user", "password")
or die("< p>Unable to connect to the database server.< /p>"
. "< p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "< /p>";

4.Add the following statements to the end of the script section to connect to the guestbook database. If the database does not exist, a message prints that the guest book does not contain any entries.

$DBName = "guestbook";
if (!@mysqli_select_db($DBConnect, $DBName))
die("< p>There are no entries in the guest book!< /p>");

5.Add the following statements to the end of the script section to select all the records in the visitors table. If no records are returned, a message prints that the guest book does not contain any entries.

$TableName = "visitors";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
if(mysqli_num_rows($QueryResult) == 0)
die("< p>There are no entries in the guest book!< /p>");

6.Add the following statements to the end of the script section to print the returned from the visitors table:

echo "< p>The following visitors have signed our guest book:";
echo "< table width='100%' border='1'>";
echo "< tr>< th>First Name< /th>< th>Last Name< /th>< /tr>";
$Row = mysqli_fetch_assoc($QueryResult);
do {
echo "< tr>< td>{$Row['first_name']}< /td>";
echo "< tr>< td>{$Row['last_name']}< /td>";
$Row = mysqli_fetch_assoc($QueryResult);
} while($Row);

7.Add the following statements to the end of the script section to close the database connection and the result pointer:

mysqli_free_result($QueryResult);
mysqli_close($DBConnect);

8.Save the document as ShowGuestBook.php in the Projects directory for Chapter 9.

9.Return to the GuestBook.html document in your text editor and add the following text and elements to the end of the document body:

< p>< a href="ShowGuestBook.php">Show Guest Book< /a>< /p>

10.Save the GuestBook.html file, and then open it in your Web browser by entering the following URL: http://localhost/PHP_Projects/Chapter.09/Projects/GuestBook.html

11.Close your Web browser window

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.