|
|
Subject:
Perl script to insert data from HTML form to MySQL
Category: Computers > Programming Asked by: omniscientbeing-ga List Price: $10.00 |
Posted:
05 Nov 2002 17:52 PST
Expires: 06 Nov 2002 14:28 PST Question ID: 99934 |
I would like a simple, "live code" example of how to populate a table I have created in a MySQL with data collected from on online, HTML form. I can find tons of examples on how to populate a MySQL database if you're sitting there at the MySQL server, using the INSERT comman, and tons of examples on how to query the/b using Perl to pull info out of it. But alas, I have been unable to find a clear, simple example of how to send the data collected rom an HTML form into the d/b. So, user enters data into form on website, and clicks Submit button. This button runs the ACTION = "www.myserver.home/cgi-bin" This is a LAMP setup (Linux Apache MySQL Perl). It's Linux Redhat 7, don't know which Apache version offhand, MySQL 3.?, and Perl 5.001, I'm pretty sure. I have on my company Intranet a simple HTML <form> with 5 fields, (FNAME, LNAME, MIDINIT, EMAIL, PHONE). Using the HTML <METHOD = "POST" ACTION = "www.myserver.home/cgi-bin/myPerlscript", I would like to know what Perl code I can use to poplate the database with. So, web user enters info to form and clicks Submit, trigering ACTION which specifies the location and name of the Perl script to run. My d/b and Table/ffields are already created and wating (unpopulated) in MySQL. I simply want to know what Perl code to write to be able to populate the database. No tutorial links, or technological alternatives--working Perl code only will contitute an accepted Answer. Thank you very much, ~omniscientbeing-ga |
|
There is no answer at this time. |
|
Subject:
Re: Perl script to insert data from HTML form to MySQL
From: edschmoe-ga on 05 Nov 2002 20:39 PST |
Firstly, you will need to install the DBI and DBD::mysql modules off of cpan. There are plenty of resources to show you how to do that. #!/usr/bin/perl use DBI; use CGI; $q = new CGI; $fname = $q->param('FNAME'); $lname = $q->param('LNAME'); $midinit = $q->param('MIDINIT'); $email = $q->param('EMAIL'); $phone = $q->param('PHONE'); $dbquery = "insert into tableName (FNAME,LNAME,MIDINIT,EMAIL,PHONE) " . "values ('$fname','$lname','$midinit','$email','$phone')"; $dbh = DBI->connect("DBI:mysql:mysql", "user", "pass"); $sth = $dbh->prepare($dbquery); $sth->execute(); $sth->disconnect(); This is a very simple prog to do what you ask... there are some other things you might consider such as data formatting (phone number for instance) or error trapping $SIG{__WARN__} but above is a quick and dirty solution. |
Subject:
Re: Perl script to insert data from HTML form to MySQL
From: omniscientbeing-ga on 06 Nov 2002 07:17 PST |
Thank you for your help. Where in this code is the name of the database specified? Mt d/b is named "test." |
Subject:
Re: Perl script to insert data from HTML form to MySQL
From: omniscientbeing-ga on 06 Nov 2002 07:24 PST |
OK, I ran this and I get the following error recorded in the server log: "Exec format error: exec of /var/www/cgi-bin/Perlinsert.pl failed" and "client [IP address]Premature end of script headers: /var/www/cgi-bin/Perlinsert.pl" Any ideas? |
Subject:
Re: Perl script to insert data from HTML form to MySQL
From: edschmoe-ga on 06 Nov 2002 08:31 PST |
>> client [IP address]Premature end of script headers: Could mean many things.... first try (from the commandline) running perl -c /var/www/cgi-bin/Perlinsert.pl If the perl interpreter says syntax ok then.... Make sure permissions on /var/www/cgi-bin/Perlinsert.pl are 755 If using suexec make sure user and group ownership are correct If you are trying to print text out to the browser, then you mus send appropiate http headers first print "Content-type: text/html\n\n"; otherwise if the perl interpreter produce errors when running perl -c.... please post here |
Subject:
Re: Perl script to insert data from HTML form to MySQL
From: edschmoe-ga on 06 Nov 2002 08:33 PST |
Oh. Also make sure you path to perl is correct. try on commandline which perl path produced should be first line of script #!/path/to/perl |
Subject:
Re: Perl script to insert data from HTML form to MySQL
From: omniscientbeing-ga on 06 Nov 2002 09:30 PST |
Path to Perl is correct. Problem apepars to be with headers. |
Subject:
Re: Perl script to insert data from HTML form to MySQL
From: asuresh-ga on 06 Nov 2002 09:43 PST |
use Mysql; #Connect with Database $dbh = Mysql->connect("localhost", "dbname", "username", "password"); #Insert Into Parent Table $sql = "INSERT <Table name>(Col1,col2,col3...) values ('$fname','$lname','$midinit','$email','$phone')"; $sth = $dbh->query($sql); The above will do it. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |