[ Team LiB ] |
7.11 Project�CGI/MySQL/DBINow we have all the necessary tools to do what we came here to do: hook DBI into CGI programs. We'll create a CGI program that displays the current contents of the age_information table like that created in Chapter 5. It also presents the user with a form to fill out, and if the user submits the form, the data submitted is added to the database table. The program is in the file /var/www/cgi-bin/age.cgi. The entire contents can be found at either http://localhost/mysql/age.cgi or www.opensourcewebbook.com/mysql/age.cgi. This program generates a page that has the look and feel of our web site, so it builds a bunch of HTML code. We put this code into two functions, top_html() and bottom_html(), to build the HTML for the top of the page and the bottom of the page, respectively. The HTML for the top of the page is quite involved because it includes the <head> information (such as metainformation) and the links along the left rail. The first thing seen in age.cgi is the subroutine top_html(), which is one big here document. This is followed by the code for bottom_html(), which is one small here document. This program is a bit more complicated than the other CGI examples we've shown, and as a result, more things can go wrong. If they do, the script needs to do more things to clean up. It needs a subroutine to do that:
The subroutine handle_error() is defined at the top of this program. It takes up to three arguments: $msg, a message is printed to the browser; $dbh, a database handle; and $sth, a statement handle. These values are shifted from @_into my() variables. After printing HTML telling the user what went wrong, the subroutine finishes the statement handle, if there is one, and then disconnects from the database, if it needs to. This means that this subroutine can be called with a statement handle and/or a database handle, if it is appropriate. It then exits the CGI program gracefully, so nothing else happens. A subroutine to process the form data is then defined:
The subroutine process_form_data takes one argument, the opened database handle $dbh. The posted data is then grabbed and stored into three variables: $lname, $fname, and $age. Then, three very important checks are made. First, the program checks to see that it has received the proper number of parameters. Also, for good style (security is always good style), it checks to see that none of the data exceeds the length that can be stored in the database. This is necessary because anyone can send this program posted data that exceeds 20 characters of text�especially critical if our MySQL data type is TEXT or BLOB. It then checks to make sure that the age passed in is numeric�if not, it is not an age. If any of these checks fail, call handle_error() to handle the error. But you might say, "Hey, didn't we limit the amount of text the user can enter into the form by setting the text widget's maxsize to 20 characters?" Yes, but this program can easily be called directly by not using this form, or it would be simple to create our own form that would allow us to enter more than 20 characters. Once the data has been checked, it can be inserted into the database. And now for the main code:
After the definition of these subroutines, the script connects as usual, but now if there is an error, handle_error() is called, not die().
The code if (param) checks to see if any posted data has been received. If so, the script calls process_form_data() to gather the posted data and add it to the database.
The script then prints the header and start of the HTML, including the start of the table that displays the information in the MySQL table.
Next, the script grabs all the information from the database, looping through the result of the SELECT query and printing each record as a row in the table.
The next step is to print the form that collects information from the user to be added to the MySQL table if the client fills it out and clicks the submit button.
And finally, the bottom HTML is printed, and the program cleans up by finishing the state handler and disconnecting the database handle. To execute this code, load one of these URLs into your browser: http://localhost/cgi-bin/age.cgi or www.opensourcewebbook.com/cgi-bin/age.cgi. You should see Figure 7.14. Figure 7.14. Age information, before adding a new entryNow, add the following information:
Enter this data and click the submit button to produce the output shown in Figure 7.15. Figure 7.15. Age information, after adding a new entry
|
[ Team LiB ] |
No comments:
Post a Comment