Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old June 22nd, 2000, 08:01 PM
explore explore is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: TX
Posts: 38 explore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via AIM to explore
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;
}


Reply With Quote
  #2  
Old June 23rd, 2000, 12:16 AM
Shiju Rajan's Avatar
Shiju Rajan Shiju Rajan is offline
.Net Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2000
Location: London
Posts: 987 Shiju Rajan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 9
Send a message via MSN to Shiju Rajan Send a message via Yahoo to Shiju Rajan

########################
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..."

Reply With Quote
  #3  
Old June 23rd, 2000, 11:56 AM
explore explore is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: TX
Posts: 38 explore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via AIM to explore
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!


Reply With Quote
  #4  
Old June 23rd, 2000, 12:52 PM
bydavid bydavid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Location: USA
Posts: 67 bydavid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
i think you may have an extra semi-colon after '$birthday')

maybe it only needs to say '$birthday')");

Reply With Quote
  #5  
Old June 26th, 2000, 03:19 AM
GoofWee GoofWee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 1 GoofWee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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).]

Reply With Quote
  #6  
Old June 27th, 2000, 04:42 PM
explore explore is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: TX
Posts: 38 explore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via AIM to explore
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.

Reply With Quote
  #7  
Old July 6th, 2000, 02:53 AM
supersalo supersalo is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 2 supersalo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
>>$form_data{'zipcode'}, '$birthday');");

Ditch the semicolor after '$birthday')

What's the error you're getting?

Reply With Quote
  #8  
Old July 6th, 2000, 01:06 PM
explore explore is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: TX
Posts: 38 explore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via AIM to explore
Hi supersalo,

I got rid of the semicolor after '$birthday')
the error message is "Premature end of script headers:"

Reply With Quote
  #9  
Old July 6th, 2000, 01:27 PM
explore explore is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: TX
Posts: 38 explore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via AIM to explore
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;
}

Reply With Quote
  #10  
Old July 7th, 2000, 12:14 AM
Shiju Rajan's Avatar
Shiju Rajan Shiju Rajan is offline
.Net Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2000
Location: London
Posts: 987 Shiju Rajan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 9
Send a message via MSN to Shiju Rajan Send a message via Yahoo to Shiju Rajan

<<
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).]

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Perl and MySQL problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway