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
and
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 asmy$variablevalue=CGI::param('formfieldname')
Creating the SQL statement can be accomplished by building up a $sqlstring variable.
To open the database connection, use a statement such as
$db=DBIconnect('DBI:mysql:mydata-base','user','pw')
Sending the command to the database takes two statements:
$output=$db->prepare($sqlstring)
and
$output->execute
Reading the results uses a loop:
while ($result=$output->fetchrow()) {print "$result";}
After reading the results, you close the database connection with
$output->finish
From there, you can use print statements to create the HTML output to send back to the browser.
| Use this form to start a public discussion with other Linux World users on this article. Log In | Register for an account (Why you should) |
Note: Register to have your user name appear; otherwise your comment will show up as "Anonymous."
*Anonymous comments will only appear once they are approved by the moderator.
• Dell puts Linux and Atom in Vostro PCs
• Mozilla names best Firefox 3 add-ons
• Torvalds: Fed up with the 'security circus'
• Dell Latitude ON - big win for Linux
• Open source advocates hail appeals court ruling
LinuxWorld Conference and Expo San Francisco, August 4-7, 2008.
Linux Plumbers Conference Portland, OR, Sept. 16-19, 2008.
FreedomHEC Santa Monica, November 8-9, 2008.