Web Development Exercise 8-1

Mar 1st, 2015
RockCafe
Category:
Computer Science
Price: $5 USD

Question description

 Exercise 8-1
In this project, 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:
        Enter your name to sign our guest book
        
        First Name 
        Last Name 
  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
                        declaration,  element, document head, and
                        element. Use the strict DTD and “Sign Guest Book” as

                       the content of the  element.

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

                  6.   Add the following statements to the script section to ensure
                       that visitors enter their first and last names:
                       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.";

                  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 "Unable to connect to the database
                                      server."
                                      . "Error code " . mysql_errno()
                                      . ": " . mysql_error() . "";

                  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 "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);

 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   489
      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), first_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) .
                        "";

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['first_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);
            }
      }
 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:///PHP_Projects/

                        Chapter.08/Projects/GuestBook.html. Test the form to see if
                        you can add your name to the database.

                    13. Close your Web browser window.

Tutor Answer

(Top Tutor) Daniel C.
(997)
School: UC Berkeley
PREMIUM TUTOR

Studypool has helped 1,244,100 students

3 Reviews


Summary
Quality
Communication
On Time
Value
BlueOcean
Nov 16th, 2016
" Awesome! Exactly what I wanted. "
ashleyisgod
Oct 25th, 2016
" Top quality work from this guy! I'll be back! "
Molly_Moon
Oct 2nd, 2016
" AMAZING as always! "
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1827 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors