MySQL and Perl for the Webby Paul DuBois
MySQL and Perl for the Web provides a much-needed handbook for database and Web developers seeking an extensive and detailed guide for using the combination of MySQL and Perl to build dynamic and interactive database-backed Web sites. It shows how to use Perl's DBI database access module, pairing it with with the CGI.pm module that allows Web pages and/i>… See more details below
MySQL and Perl for the Web provides a much-needed handbook for database and Web developers seeking an extensive and detailed guide for using the combination of MySQL and Perl to build dynamic and interactive database-backed Web sites. It shows how to use Perl's DBI database access module, pairing it with with the CGI.pm module that allows Web pages and forms to be generated and processed easily. These tools provide developers with a solid foundation for creating applications that incorporate database content to create dynamic, up-to-date Web sites.
The book employs a practical learn-by-doing approach that demonstrates development techniques by building complete applications, not just fragmentary pieces. It shows what to do at each step of the way during the process of building live, working examples. Applications are fully explained so you can understand how they work and apply the techniques they illustrate to your own projects.
Read an Excerpt
Chapter 5: Writing Form-Based ApplicationsChapter 4 "Generating and Processing Forms," covered the general techniques involved in creating forms. In this chapter, we'll use those techniques to write several types of form-based applications, and in Chapter 6, "Automating the Form- Handling Process," we'll discuss how you can use information in your database to help you handle forms automatically.
Web programming includes such a diverse range of applications that we can't hope to cover more than a fraction of the possibilities. Nevertheless, there are several recur- ring issues, and the applications in this chapter illustrate a number of useful techniques that you should be able to apply to many of your own projects. Therefore, although useful in their own right, these applications are not just ends in themselves —they serve an illustrative purpose as well. The projects we'll tackle, and some of the techniques they involve, are as follows:
- A product-registration script that enables customers to register purchases online
by visiting your Web site rather than by mailing in a paper form. It shows how
to generate and validate a form on the basis of specifications stored in a data
- A guestbook. I guess every Web programming book has to have a guestbook, so this one does, too. However, our version serves only as a means by which to demonstrate how to incorporate email capabilities into your scripts. The guestbook itself uses email to help keep you apprised of new entries, and we'll cover other ways you can use mail capabilities in your applications. The section also discusses how to set up jobs that execute according to predetermined schedule.
- A giveaway contest application that enables visitors to your site to submit contest entries. This section covers some basic fraud-detection techniques to help combat ballot-box stuffing, selection of random entries to choose contest winners, and entry summary and expiration methods.
- A simple poll. We'll develop a script that uses a form to present candidates users can vote for, and that displays a results page showing the current vote totals. The application uses MySQL to count the votes, and the results page uses the current totals in the database so that the results shown are always up to date.
- Image-storage and image-display scripts. Images are an integral part of many Web applications, which necessitates a method for getting them to your server host and accessing them from within your scripts. To provide support for image use, we'll illustrate how to load images into MySQL two ways:over the Web using a form containing a file-upload field, and from the command line. In addi- tion, this section shows how to display images in Web pages by pulling them from your database.
- An application that enables you to construct an electronic greeting card interactively and notify the recipient that it's waiting. The card is stored as a database record so that it can be retrieved and displayed later for the recipient. The application also notifies you when the recipient views the card. This application incorporates image-display capabilities and shows how to implement multiple- stage record construction, how to trigger a notification when a record's status changes, and how to handle removal of expired cards.
- Generate a form to solicit the information you want to collect from the user. Some forms are relatively trivial: Our polling application presents a form containing nothing more than a set of radio buttons listing the candidates and a Submit button. The user clicks one time to pick a candidate, a second time to submit the vote, and that's it. Other forms are more extensive: The product- registration application has many fields because we need to gather information about both the product being registered and the user who's registering it.
Validate the form's contents when the user fills it in and submits it. Form validation can be minimal or quite extensive. You may have fields that are required but
were left blank by the user, or fields that must contain a certain kind of information but were filled in incorrectly. In such cases, the user must submit additional
or revised information. You'll find it necessary to inform the user that the form
cannot be processed, as well as what should be done to correct any problems.
We'll demonstrate several feedback techniques over the course of the chapter.
- Store the form submission. Some applications store form information in a file or mail it somewhere for further processing. This being a book on MySQL, we will of course focus on using a database as the primary storage mechanism for each application.
- Generally, you provide some kind of feedback to the user after a form submis- sion has been received and processed. This can be quite simple, such as a thank you message expressing appreciation for the user's participation in a poll or survey, or a brief acknowledgement that the submission was received. Or you may redisplay the information to provide confirmation to the user that it was received properly.
You may find it useful to install each script and try it out first before you read the section that describes how it works.
Product RegistrationRegistration applications serve many purposes. You can use them to allow people to register products they have purchased, sign up for conferences, request a catalog, add themselves to a mailing list, and so forth. The obvious advantage over paper forms for you when you receive a registration from someone is that you needn't re-key the information to get it into your database —the Web application inserts it for you. An advantage for users is instant transmission of their registration information without having to dig up a stamp or put the registration form in the mail.
In this section, we'll write a script, prod_reg. pl , that collects product registrations over the Web. Generally, this kind of registration form has, at a minimum, fields to identify the product and the customer, and our application will confine itself to gathering that kind of information. Many registration forms have additional fields for demographic information such as household income, type of employment, or how the product will be used. We'll skip that stuff;you can add it later if you like.
To process registrations, we need a database table in which to store registration records and an application that collects information from customers and inserts records into the table. The table we'll use looks like this...
Meet the Author
Paul DuBois began his involvement with MySQL after recoiling in horror at the complexities of dealing with a database from one of the larger commercial vendors, and with its customer support mechanism. Turning to MySQL for relief proved to have unforeseen and unexpected consequences: first as the opportunity to contribute to the MySQL Reference Manual, then to writing "MySQL" with New Riders, and most recently to his present employment with NuSphere, a company actively involved in MySQL development, promotion, and training.
Paul's responsibilities and interests have at one time or another involved database development, Web site development and management, mailing list management, system administration, and TCP/IP and AppleTalk networking. He's considered a leader in the MySQL and Open Source communities.
and post it to your social network
Most Helpful Customer Reviews
See all customer reviews >