• United States

Updating a MySQL database with Perl

Dec 12, 20052 mins
Enterprise ApplicationsMySQL

I need to update a MySQL database in response to a Web form submission that uses Perl. I know the basics of Perl, and the Web site provides the database connectivity modules for Perl, but I need some help. What are the basic steps for performing a database update with Perl?

This involves gathering up the form field data from the submission, constructing the SQL statement you want to send to the database, opening the connection, sending the command, reading the results and delivering a response to the user.

You will want to include the statements

use CGI


use DBI

at the beginning of your script so you can easily read the form fields and talk to the database. Form data is retrieved by using statements such as


Creating the SQL statement can be accomplished by building up a $sqlstring variable.

To open the database connection, use a statement such as


Sending the command to the database takes two statements:




Reading the results uses a loop:

while ($result=$output->fetchrow()) {print "$result";}

After reading the results, you close the database connection with


From there, you can use print statements to create the HTML output to send back to the browser.