newtoperl101 has asked for the wisdom of the Perl Monks concerning the following question:
Hi,(NEW UPDATED QUESTION) I wrote a PERL .cgi script to connect to database. I also created a html form for user input. The idea is to pass parameters from HTML form to CGI. However, when I run the html file from the browser it simply publishes the script instead of passing parameters. Yes, I have configured Apache for CGI and gave write permissions to my files. The cgi file is in cgi-bin and the html file in htdocs. Please help why this isn't working. Here is my html file contactsform.html:
<html> <head><title>ADDRESS BOOK</title></head> <body bgcolor="#FFFFFF" link="#0000FF" alink="#FF0000" vlink="#C000FF" +> <h1>ADDRESS BOOK CONTACTS</h1> <table> <form method="post" action="C:/Apache/Apache2/cgi-bin/sqlconfig.cgi"> <tr> <td align="right">Last Name:</td> <td align="left"><input type="text" name="lname" size="15" maxlength +="50"></td> <td align="right">First Name:</td> <td align="left"><input type="text" name="fname" size="15" maxlength +="50"></td> </tr> <tr> <td align="right">Phone:</td> <td align="left"><input type="text" name="phone" size="15" maxlength +="50"></td> <td align="right">Email:</td> <td align="left"><input type="text" name="email" size="15" maxlength +="50"></td> </tr> <tr> <td align="right">Address:</td> <td align="left"><input type="text" name="address" size="15" maxleng +th="50"></td> <td align="right">Zip Code:</td> <td align="left"><input type="text" name="zip" size="15" maxlength=" +50"></td> </tr> </table> <input type="submit" value="Submit"> </form> </body> </html>
And this is the perl script sqlconfig.cgi:
#!c:/Dwimperl/perl/bin/perl.exe # PERL MODULES WE WILL BE USING use CGI; use DBI; use DBD::mysql; # Config DB variables our $platform = "mysql"; our $database = "test"; our $host = "localhost"; our $port = "3306"; our $tablename = "addressbook"; our $user = "root"; our $pw = "password"; our $q = new CGI; # DATA SOURCE NAME $dsn = "dbi:mysql:$database:localhost:3306"; # PERL DBI CONNECT $connect = DBI->connect($dsn, $user, $pw); #Get the parameter from your html form. $lname=$q->param('lname'); $fname=$q->param('fname'); $phone=$q->param('phone'); $email=$q->param('email'); $address=$q->param('address'); $zip=$q->param('zip'); print $q->header; $sql="INSERT INTO test.addressbook(last_name,first_name) values('$lnam +e','$fname')"; $sth = $connect->prepare($sql) or die "Can't prepare $sql: $connect->errstrn"; #pass sql query to database handle.. $rv = $sth->execute or die "can't execute the query: $sth->errstrn"; #execute your query if ($rv==1){ print "Record has been successfully updated !!!n"; }else{ print "Error!!while inserting recordn"; exit; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to bind html page with PERL script to accept user input to Mysql database (eg. Phonebook)
by ww (Archbishop) on Jul 04, 2013 at 00:44 UTC | |
by newtoperl101 (Novice) on Jul 04, 2013 at 00:48 UTC | |
by ww (Archbishop) on Jul 04, 2013 at 00:54 UTC | |
by Shuraski (Scribe) on Jul 04, 2013 at 15:30 UTC | |
by poj (Abbot) on Jul 04, 2013 at 08:50 UTC | |
by newtoperl101 (Novice) on Jul 04, 2013 at 17:32 UTC |