Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > SCRIPT WEB DATABASES QUICKLY WITH PHP


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Script Web databases quickly with PHP
By Craig Knudsen - 1999-09-01 Page:  1 2 3 4 5 6 7 8

Form Processing

Form processing under PHP is simple. Listing 5 and Figure 1 show an example of a form for adding a new entry to the address book. A simple table lines up the text-entry fields.

Listing 5. A script to create the form shown in Figure 1


Listing 5
 1   <HTML><HEAD>

 2   <TITLE>Address Book Edit</TITLE>
 3   </HEAD><BODY>
 4   <H1>Edit</H1>

 5   <FORM ACTION="add_handler.php3" METHOD="POST">
 6   <TABLE border=0>
 7   <TR><TD><B>Last Name:</B></TD>

 8     <TD><INPUT name="last_name" SIZE=30></TD></TR>
 9   <TR><TD><B>First Name:</B></TD>
10     <TD><INPUT name="first_name" SIZE=30></TD></TR>

11   <TR><TD><B>Email:</B></TD>
12     <TD><INPUT name="email" SIZE=40></TD></TR>
13   </TABLE><P>

14   <INPUT TYPE="submit" VALUE="Save">
15   </FORM>
16   <P><HR>
17   <A href="index..php3">Back to Index</A>
18   </BODY></HTML>

Form for adding a new entry

The form in Listing 5 specifies the action add_handler.php3, which is shown in Listing 6.

Listing 6 introduces some new topics: form handling, adding new rows to a database, error handling, and redirecting.

The form contains three form elements in new.html : last_name, first_name, and email. To access the value the user entered as the last_name form element, the variable $last_name is used. In this example, I submit the form using the HTTP post method. Use the same technique when using get rather than post .

Line 7 of Listing 6 retrieves the value for the form element last_name, using the following logic:

  1. Use the empty function to determine if the user left the last name blank. If so, then set $last_name to "NULL" .
  2. Otherwise, use the addslashes function to add the appropriate backslashes for quotes in the last name and place the last name in double quotes. This prevents the script from submitting invalid SQL requests to MySQL.

Conditional syntax just like that found in C, Perl, and Java accomplishes the retrieval on lines 7 and 8. After processing the first name and e-mail data in the same manner, line 14 creates the SQL INSERT command to add the new entry to the database. There's no value for the ID column because the AUTO_INCREMENT feature of MySQL was used to create the PERSON table. The ID field automatically will have the next available integer.

Line 16 sends the INSERT command to the database. If the request is successful, it returns a positive value, and the user is redirected to the index page index.php3 , using the redirect function on line 19. (Note that the redirect function needs to be called before any HTML output is sent.) The redirect causes processing of this page to end, and an HTTP redirect header will be sent.

The script handles errors by saving the error message to the $error variable, using the mysql_error function, and then not performing the redirect. This allows the HTML at the end of the script to be displayed. In the event of an error, lines 28 through 30 cause the display of an error message and the SQL command that caused the problem.

The downloadable version of the code example (see Resources) includes some additional pages (editing and deleting entries). They do not introduce any additional concepts, so they aren't included here in the article.

Listing 6. Processing a submitted form



Listing 6
 1   <?php
 2   // Connect to database.
 3   $c = mysql_pconnect ( "localhost", "mylogin", "mypasswd" ) ||
 4     die ( "Error connecting to database!" );
 5   // Add a backslash to all characters that would confuse SQL.
 6   // Use NULL for values that were left empty.
 7   $last_name =
 8     empty ( $last_name ) ? "NULL" : "'" . addslashes ( $last_name ) . "'";
 9   $first_name =
10     empty ( $first_name ) ? "NULL" : "'" . addslashes ( $first_name ) . "'";
11   $email =
12     empty ( $email ) ? "NULL" : "'" . addslashes ( $email ) . "'";
13   // Add new entry and redirect to index page.
14   $sql = "INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, EMAIL ) ( " .
15     "$last_name, $first_name, $email )";
16   if ( ! mysql_db_query ( "ADDRESS_BOOK", $sql ) ) {
17     $error = mysql_error ();
18   } else {
19     Header ( "Location: index.php3" );
20     exit;
21   }
22   ?>
23   <HTML><HEAD>
24   <TITLE>Address Book Error</TITLE>
25   </HEAD><BODY>

26   <H1>Error</H1>
27   <?php
28   print ( $error );
29   if ( strlen ( $sql ) )
30     printf ( "<P><B>SQL:</B><BLOCKQUOTE><TT>%s</TT></BLOCKQUOTE>", $sql );
31   ?>

32   </BODY>
33   </HTML>


View Script Web databases quickly with PHP Discussion

Page:  1 2 3 4 5 6 7 8 Next Page: Maintaining Sessions

First published by IBM developerWorks


Copyright 2004-2017 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.