Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting into mysql database 1

Status
Not open for further replies.

riffy

Programmer
Joined
Mar 29, 2001
Messages
106
Location
US
hi everyone,

hope someone can help...i have a mail registration page where the user fills out his/her information and then submits the form...i tried using php but i couldn't get the info to be entered into the database, now i thought maybe i'll use perl and see what happens...but same thing, the information is not entered into the db...

basically the mail page contains 3 text boxes, one each for first name, last name, and email address, a drop down list to select country, a check box to receive email updates, 3 more check boxes to choose what the person is interested in and another drop down to select an area of interest...

so in total there are 9 fields...3 text boxes, 2 drop down lists, and 4 checkboxes...

of course i have named them and given a value of 1 to the checkboxes...

i'm posting my perl code to process the information...let me know if i need anything else...
Code:
#!/usr/local/bin/perl

use CGI qw(:standard);           
use DBI;                         

my $dbh = DBI->connect("DBI:mysql:mailform:localhost", "root", "") or die "Can't get DBH: $DBI::errstr";      

$query = new CGI;              

print "Location: thankyou.shtml\n\n";
#print "Content-type: text/html\n\n";


$name=$query->param('fname');
$lname=$query->param('lname');
$email=$query->param('email');
$country=$query->param('country');
$oktoemailme=$query->param('email_ok');
$feature=$query->param('feature_stories');
$light=$query->param('how_to_light');
$news=$query->param('site_news');
$job=$query->param('occupation');

$sql= "insert into user (fname, lname, email, country, oktoemailme, feature, light, news, job) values (?,?,?,?,?,?,?,?,?)";


my  $sth = $dbh->prepare("$sql") or return;                              
$sth->execute($fname, $lname, $email, $country, $oktoemailme, $feature, $light, $news, $job,);

$autoid= $sth->{"mysql_insertid"};

if someone can help, please do...

thanks a whole bunch...
arif X-) s-) (-:
 
Hey:

I answered your post in the PHP forum so go check that.

Some things about your Perl code:

1. The connect is not right:
You have:
[tt]
my $dbh = DBI->connect("DBI:mysql:mailform:localhost", "root", "") or die "Can't get DBH: $DBI::errstr";
[/tt]
Should be:
[tt]
my $dbh = DBI->connect("DBI:mysql:mailform","localhost", "root", "") or die "Can't get DBH: $DBI::errstr";
[/tt]
Where localhost is a seperate argument.

2. Your query is bad:

You have:
[tt]
$sql= "insert into user (fname, lname, email, country, oktoemailme, feature, light, news, job) values (?,?,?,?,?,?,?,?,?)";
[/tt]
Should be:
[tt]
$sql= "insert into user (fname, lname, email, country, oktoemailme, feature, light, news, job) values ('value1','value2','value3','value4','value5','value6','valu
e6','value7','value8')";
[/tt]

And finally:
The execute() function requires no parameters. Just call the prepare($sql) function and then the execute() function.

I think thats it.

Hope this helps.

-Vic
vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Vic, unless I'm missing something, riffy's use of placeholders in his prepare looks OK to me, and so does his use of corresponding parameters in his execute.

Riffy, does the connect work OK? Are you getting a good $dbh? I notice you didn't set "RaiseError", so you should be checking each DBI statement for errors, but you're not. If your prepare fails, you're just returning - you should print an error to see if that is what's failing, *AND* return. Read up on what "prepare" returns by reading the DBI perldoc(perldoc DBI). Make sure you put some similar error check in your "execute".

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
hey thanks alot guys for ure comments and suggestions but the mistake was a real silly one on my part...i checked the error log for the site i'm working on and i realized that the variables i specified in perl weren't the same ones as the field names in the database so nothing would get inserted...

everything is fine now though...
thanks anywayz
arif X-) s-) (-:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top