Question: In this exercise, you will create a Web page that allows visitors to your site to sign a guest book that is saved to a

In this exercise, you will 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 declaration, element, document head, and element. Use the strict DTD and Guest Book as the content of the element. 2. Add the following text and elements to the document body: <h2>Enter your name to sign our guest book</h2> <form method="POST" action="SignGuestBook.php"> <p>First Name <input type="text" name="fi rst_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 8. 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 Sign 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($_POST['fi rst_name']) || empty($_ POST['last_name'])) echo "<p>You must enter your fi rst and last name! Click your browser'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 host with the host name of your MySQL server, and user and password with the MySQL user name and password you created in Chapter 7. else { $DBConnect = @mysql_connect("host", "user", "password"); if ($DBConnect === FALSE) echo "<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysql_errno() . ": " . mysql_error() . "</p>"; 8. Add the following statements to the end of the script section to create a database named guestbook if it does not already exist: else { $DBName = "guestbook"; if (!@mysql_select_db($DBName, $DBConnect)) { $SQLstring = "CREATE DATABASE $DBName"; $QueryResult = @mysql_query($SQLstring, $DBConnect); if ($QueryResult === FALSE) echo "<p>Unable to execute the query.</p>" . "<p>Error code " . mysql_ errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>"; else echo "<p>You are the fi rst visitor!</p>"; } mysql_select_db($DBName, $DBConnect); 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 auto-incrementing primary key field named countID.$TableName = "visitors"; $SQLstring = "SHOW TABLES LIKE '$TableName'"; $QueryResult = @mysql_query($SQLstring, $DBConnect); if (mysql_num_rows($QueryResult) == 0) { $SQLstring = "CREATE TABLE $TableName (countID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, last_name VARCHAR(40), fi rst_name VARCHAR(40))"; $QueryResult = @mysql_query($SQLstring, $DBConnect); if ($QueryResult === FALSE) echo "<p>Unable to create the table.</p>" . "<p>Error code " . mysql_ errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>"; 10. Finally, add the following statements to the end of the script section. These mysql_query() statements add the visitor to the database. The last statement closes the database connection. $LastName = stripslashes($_ POST['last_name']); $FirstName = stripslashes($_ POST['fi rst_name']); $SQLstring = "INSERT INTO $TableName VALUES(NULL, '$LastName', '$FirstName')"; $QueryResult = @mysql_ query($SQLstring, $DBConnect); if ($QueryResult === FALSE) echo "<p>Unable to execute the query.</p>" . "<p>Error code " . mysql_ errno($DBConnect) . ": " . mysql_ error($DBConnect) . "</p>"; else echo "<h1>Thank you for signing our guest book!</h1>"; } mysql_close($DBConnect); } } 11. Save the document as SignGuestBook.php in the Projects directory for Chapter 8. Upload both SignGuestBook.php and GuestBook.html to the server. 12. Open GuestBook.html in your Web browser by entering the following URL: http://<yourserver>/PHP_Projects/ Chapter.08/Projects/GuestBook.html. Test the form to see if you can add your name to the database. 13. Add two more fake entries into the guest book. 14. Login to MYSQL Monitor enter the following command to view all the records in the visitors table: mysql> SELECT * FROM visitors;[ENTER ]</p> <p> ---------------------------------------------------------------------------------------------------------------------------</p> <p>This is what I have but I can't seem to open the GuestBook.html file? How do you figure out my "server name"? And is that how you do "Strict DTD, I've never done that before? I just need someone to check this and help me out!</p> <p>GuestBook.html</p> <p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"></p> <p><!-- This website template was created by: Kadie Kimbrough --></p> <p><html lang="en"></p> <p><head></p> <p><title>Guest Book

Enter your name to sign our guest book

First Name

Last Name

-----------------------------------------------------------------------------------------

SignGuestBook.php

Sign Guest Book

if (empty($_POST['first_name']) || empty($_POST['last_name']))

echo "

You must enter your first and last name! Click your

browser's Back button to return to the Guest Book form.

";

else {$DBConnect = @mysql_connect("localhost", "root","password");

if ($DBConnect === FALSE)

echo "

Unable to connect to the database server.

".

"

Error code " . mysql_errno(). ": " . mysql_error() . "

";

else {$DBName = "guestbook";

if (!@mysql_select_db($DBName, $DBConnect))

{

$SQLstring = "CREATE DATABASE $DBName";

$QueryResult = @mysql_query($SQLstring,$DBConnect);

if ($QueryResult === FALSE)

echo "

Unable to execute the query.

".

"

Error code " . mysql_errno($DBConnect). ":

" . mysql_error($DBConnect). "

";

else

echo "

You are the first visitor!

";

}

mysql_select_db($DBName, $DBConnect);

$SQLstring = "SHOW TABLES LIKE '$TableName'";

$QueryResult = @mysql_query($SQLstring, $DBConnect);

if (mysql_num_rows($QueryResult) == 0)

{

$SQLstring = "CREATE TABLE $TableName

(countID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,

last_name VARCHAR(40), fi rst_name VARCHAR(40))";

$QueryResult = @mysql_query($SQLstring,$DBConnect);

if ($QueryResult === FALSE)

echo "

Unable to create the table.

". "

Error code

" . mysql_errno($DBConnect). ": " . mysql_error($DBConnect) ."

";

$LastName = stripslashes($_POST['last_name']);

$FirstName = stripslashes($_POST['fi rst_name']);

$SQLstring = "INSERT INTO $TableName VALUES(NULL, '$LastName',

'$FirstName')";

$QueryResult = @mysql_query($SQLstring, $DBConnect);

if ($QueryResult === FALSE)

echo "

Unable to execute the query.

". "

Error code

" . mysql_errno($DBConnect). ": " . mysql_error($DBConnect) . "

";

else

echo "

Thank you for signing our guest book!

";

}

mysql_close($DBConnect);

}

}

?>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!