|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Help! I have written what I thought was a simple form and perl script to out put the form data from the web into mysql and it isn't working..can someone tell me where I went wrong..Thanks in advance!
Here's the html: <HTML> <HEAD><TITLE>This the Address Book Form</TITLE></HEAD> <BODY> <FORM ACTION="/cgi/addentry.pl" METHOD=POST> First Name: <INPUT TYPE=text NAME=first_name SIZE=20 MAXLENGTH=20><BR> Last Name: <INPUT TYPE=text NAME=last_name SIZE=20 MAXLENGTH=20><BR> Phone Number: <INPUT TYPE=text NAME=phone SIZE=15 MAXLENGTH=15><BR> Address: <INPUT TYPE=text NAME=address SIZE=20 MAXLENGTH=100><BR> City: <INPUT TYPE=text NAME=city SIZE=20 MAXLENGTH=20><BR> State: <INPUT TYPE=text NAME=state SIZE=2 MAXLENGTH=2><BR> Zip Code: <INPUT TYPE=text NAME=zipcode SIZE=5 MAXLENGTH=5><BR> Birthday (MM-DD-YYYY): <INPUT TYPE=text NAME=month SIZE=2 MAXLENGTH=2>- <INPUT TYPE=text NAME=day SIZE=2 MAXLENGTH=2>- <INPUT TYPE=text NAME=year SIZE=4 MAXLENGTH=4><BR> <INPUT TYPE=submit><INPUT TYPE=reset> </FORM> </BODY> </HTML> Here's the perl script: #!/usr/bin/perl require "dbi-lib.pl"; #----------------------# # User Variables # #----------------------# $table_name = "addressbook"; #----------------------# # Main Body # #----------------------# &parse_input; &print_header; if (&check_input) { &initialize_dbi; $birthday = &birthday; &run_statement("insert into $table_name values('$form_data{'first_name'}', '$form_data{'last_name'}', '$form_data{'phone'}', '$form_data{'address'}', '$form_data{'city'}', '$form_data{'state'}', $form_data{'zipcode'}, '$birthday');"); print "Data was successfully enteredn"; } else { print "Data was NOT entered due to errors.n"; } exit; #----------------------# # Functions # #----------------------# sub check_input { if ($form_data{'first_name'} && $form_data{'last_name'} && $form_data{'phone'} && $form_data{'address'} && $form_data{'city'} && $form_data{'state'} && $form_data{'zipcode'} && $form_data{'month'} && $form_data{'day'} && $form_data{'year'}) { return 1; } return 0; } sub birthday { local $birthday = $form_data{'year'}."-".$form_data{'month'}."-".$form_data{'day'}; return $birthday; } |
|
#2
|
||||
|
||||
|
######################## require "dbi-lib.pl"; ######################## Show me the "dbi-lib.pl". You should check it out wether this connectivity script is working properly or not with help of a small select statement. insert statement should issue like: "INSERT INTO tblname(fieldname1,fieldname2,fieldname3) values('value1','value2','value3')"; ------------------ SR - shiju.dreamcenter.net "The fear of the LORD is the beginning of knowledge..." |
|
#3
|
|||
|
|||
|
Thanks SR...here is the dbi file:
(the correction of the insert statement didn't fix the problem)#!/usr/bin/perl $user_name = "myusername"; $user_password = "mypassword"; $sql_server = "mysqlserver"; sub initialize_dbi { use DBI; $drh = DBI->install_driver( 'mysql' ); $dbh = DBI->connect("DBI:mysql:$user_name:$sql_server", $user_name, $user_password); die "Cannot connect: $D: $DBI::errstrn" unless $dbh; } sub run_statement { $stmt = "$_[0]"; $sth = $dbh->prepare($stmt); $sth->execute; } sub parse_input { $whichmethod = $ENV{'REQUEST_METHOD'}; if($whichmethod eq "GET"){ $forminfo = $ENV{"QUERY_STRING"}; }else{ $forminfo = <STDIN>; } @key_value_pairs = split(/&/,$forminfo); foreach $pair (@key_value_pairs){ ($key,$value) = split(/=/,$pair); $value =~ s/+/ /g; $value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg; $value =~ s/'/'/g; $FORM_DATA{$key} = $value; } } sub print_header { print "Content-type: text/html nn"; } return true; Thanks sooo much for your help thus far! |
|
#4
|
|||
|
|||
|
i think you may have an extra semi-colon after '$birthday')
maybe it only needs to say '$birthday')"); |
|
#5
|
|||
|
|||
|
I thought, $dbh = DBI->connect("DBI:mysql:$user_name:$sql_server", $user_name, $user_password);
was supposed to be $dbh = DBI->connect("DBI:mysql:$database_name:$sql_server", $user_name, $user_password); or am I wrong? [This message has been edited by GoofWee (edited June 26, 2000).] |
|
#6
|
|||
|
|||
|
GoofWee, that didn't work for me to change that information.
Does anyone else have any suggestions. I am still unable to get this to work. |
|
#7
|
|||
|
|||
|
>>$form_data{'zipcode'}, '$birthday');");
Ditch the semicolor after '$birthday') What's the error you're getting? |
|
#8
|
|||
|
|||
|
Hi supersalo,
I got rid of the semicolor after '$birthday') the error message is "Premature end of script headers:" |
|
#9
|
|||
|
|||
|
I changed a couple of things now I get the error message "Data was NOT entered due to errors."
here is the modified file that generated the error: #!/usr/bin/perl require "dbi-lib.pl"; #----------------------# # User Variables # #----------------------# $table_name = "addressbook"; #----------------------# # Main Body # #----------------------# &parse_input; &print_header; if (&check_input) { &initialize_dbi; $birthday = &birthday; &run_statement("insert into $table_name(first_name,last_name,phone,address,city,state,zipcode,birthday) VALUES('$form_data{'first_name'}', '$form_data{'last_name'}', '$form_data{'phone'}', '$form_data{'address'}', '$form_data{'city'}', '$form_data{'state'}', $form_data{'zipcode'}, '$birthday')"); print "Data was successfully enteredn"; } else { print "Data was NOT entered due to errors.n"; } exit; #----------------------# # Functions # #----------------------# sub check_input { if ($form_data{'first_name'} && $form_data{'last_name'} && $form_data{'phone'} && $form_data{'address'} && $form_data{'city'} && $form_data{'state'} && $form_data{'zipcode'} && $form_data{'month'} && $form_data{'day'} && $form_data{'year'}) { return 1; } return 0; } sub birthday { local $birthday = $form_data{'year'}."-".$form_data{'month'}."-".$form_data{'day'}; return $birthday; } |
|
#10
|
||||
|
||||
|
<< else { print "Data was NOT entered due to errors.n"; } >> this means your "check_input" is returning "0".just check it out wether all the field values are passing to your program or not.. sub check_input { if ($form_data{'first_name'} && $form_data{'last_name'} && $form_data{'phone'} && $form_data{'address'} && $form_data{'city'} && $form_data{'state'} && $form_data{'zipcode'} && $form_data{'month'} && $form_data{'day'} && $form_data{'year'}) { return 1; } return 0; } also ...what is the return "0" here .that is out of "if condition"..so it will always return "0"....change that line... ------------------ SR - webshiju.com "The fear of the LORD is the beginning of knowledge..." [This message has been edited by Shiju Rajan (edited July 06, 2000).] |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Perl and MySQL problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|