PDA

View Full Version : Inserting records with perl - helpme please


nomadsoul
08-10-2006, 06:47 PM
Im trying to insert records from html forms into a Mysql db using a perl script(see below). I've done this hundreds of times with php no problem. So, for a project Im doing, I translated the script to perl(or what I think is the correct translation). I get no syntax error from perl -c script.cgi but I do get a 500 error. I can't find any decent webtutorials to show me what to do. Kevin DID show me how to view records. Now I need to insert sucessfully.
Any help is greatly appreciated

First here's the insert.html:
<html>
<head>
</head>
<center>
<form method="post" action="http://www.myfqsite.com/cgi-bin/script.cgi">

<table>
<tr><td align="left">Name</td>
<td><input type="text" name=name></td>
</tr>
<tr><td align="left">Telephone</td>
<td><input type="text" name=telephone ></td>
</tr>
<tr><td align="left">Email</td>
<td><input type="text" name=email ></td>
</tr>
<tr><td colspan="2">
<p align="center">
<input type="submit" value="Enter record">
</td>
</tr>
</table>
</form>
</center>
</html>

and heres the process: script.cgi:
#! /usr/bin/perl

use DBI;
$DBHOST = "MySql.myfqsite.com";
$DBNAME = "xmyfqsite-contact";
$DBUSER = "xmyfqsiteusername";
$DBPASS = "mypassword;
$table = "players";

$DB= DBI->connect ('DBI:mysql:$DBNAME:$DBHOST','$DBUSER','$DBPASS');
use CGI qw(:standard);
$Form = new CGI;
$sqlquery = "INSERT INTO $table VALUES('$name','$telephone','$email')";

$results = mysql_query($sqlquery);

mysql_close();

print "<html><body><center>";
print "<p>You have just entered this record into the players table<p>";
print "Name : $name<br>";
print "Telephone : $telephone<br>";
print "Email :$email";
print "</body></html>";

Kevin
08-10-2006, 08:49 PM
Instead of:
$sqlquery = "INSERT INTO $table VALUES('$name','$telephone','$email')";
$results = mysql_query($sqlquery);
try:

$InsertString = "INSERT INTO $table VALUES('$name','$telephone','$email')";
$InsertHandle=$DB->prepare($InsertString);
$InsertHandle->execute;
Also, whenever you quote code in the forums you should wrap them in code tags to avoid mangling them with emoticons.

nomadsoul
08-10-2006, 09:43 PM
Kevin, here is how I applied it, but still getting the 500 error however the syntax is ok:
[nomadsoul@FQ-Samson:/big/dom/xprimehomesrealestate/cgi-bin ]$ perl -c script.cgi
script.cgi syntax OK

Are there any tutorials on the net(for beginners) that address this?

#! /usr/bin/perl

use DBI;
$DBHOST = "MySql.myfqsite.com";
$DBNAME = "xmyfqdbname-contact";
$DBUSER = "xmyusername";
$DBPASS = "mypassword";
$table = "players";

$DB= DBI->connect ('DBI:mysql:$DBNAME:$DBHOST','$DBUSER','$DBPASS');
use CGI qw(:standard);
$Form = new CGI;

$InsertString = "INSERT INTO $table VALUES('$name','$telephone','$email')";
$InsertHandle=$DB->prepare($InsertString);
$InsertHandle->execute;

mysql_close();

print "<html><body><center>";
print "<p>You have just entered this record into the players table<p>";
print "Name : $name<br>";
print "Telephone : $telephone<br>";
print "Email :$email";
print "</body></html>";

Kevin
08-11-2006, 12:27 AM
Check your cgi error log (in the CNC). It usually gives more information on what is wrong when there is an error.

Rich
08-11-2006, 11:32 AM
One reason for the 500 error is that no header has been output. Since you are "using" CGI add the following before your print statements:

print header;

See http://search.cpan.org/dist/CGI.pm/CGI.pm for more info.

nomadsoul
08-11-2006, 12:38 PM
Here is the error code:(I don't see anything I could interpret and fix)
%% [Fri Aug 11 11:32:03 2006] POST /cgi-bin/script.cgi HTTP/1.1
%% 500 /big/dom/xprimehomesrealestate/cgi-bin/script.cgi
%request
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding: gzip,deflate
Accept-Language: en-us,en;q=0.5
Connection: keep-alive
Content-Length: 35
Content-Type: application/x-www-form-urlencoded
Host: www.primehomesrealestate.com
Keep-Alive: 300
Referer: http://www.primehomesrealestate.com/perl/insert.html
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7

name=test&telephone=test&email=test
%response

And I used the print header statement like this at the top:
#! /usr/bin/perl

use DBI;
use CGI qw/:standard/;
print header,

and like this at the bottom before the print statements:
print header;

It still won't enter a record

nomadsoul
08-11-2006, 08:57 PM
At this point I'm just trying to test my connection using:
#!/usr/bin/perl
use DBI;
$dbh = DBI->connect("DBI:mysql:xprimehomesrealestate-contact:MySql.primehomesrealestate.com", "xmyusername", "mypassword");
print $dbh;


Why is it so difficult connect with perl and so easy with php?
error:
%% [Fri Aug 11 19:55:45 2006] GET /cgi-bin/connect.cgi HTTP/1.1
%% 500 /big/dom/xprimehomesrealestate/cgi-bin/connect.cgi
%request
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding: gzip,deflate
Accept-Language: en-us,en;q=0.5
Connection: keep-alive
Host: www.primehomesrealestate.com
Keep-Alive: 300
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7
%response

Terra
08-12-2006, 05:55 PM
Most likely the MySQL DBI connection is indeed working, however from a CGI script, you have to send the content-type before printing anything else otherwise it will blow with an error...

Try adding the following before your first print:

print("Content-Type: text/html\n\n");
print $dbh;


--
Terra
sysAdmin
FutureQuest

nomadsoul
08-13-2006, 03:14 PM
Terra,
That doesn't work either,getting the same error.
I also tried another script I found here:http://www.perlscriptsjavascripts.com/tutorials/mysql/index.html#connecting
And it still won't work for a simeple select statement, I've included the error which says I havent loaded MySql (which I did):

#!/usr/bin/perl

# PERL MODULES WE WILL BE USING
use DBI;
use DBD::mysql;
$DBHOST = "MySql.primhomesrealestate.com";
$DBNAME = "xprimehomesrealestate-contact";
$DBUSER = "xprimehomesreale";
$DBPASS = "zuluaskono2";

$DB = Mysql->connect($DBHOST, $DBNAME, $DBUSER, $DBPASS);

$DB = Mysql->connect($DBHOST, $DBNAME, $DBUSER, $DBPASS);
$qry = "SELECT * FROM guestbook ";
print("Content-Type: text/html\n\n");
while( @emps = $qry->fetchrow) {
print "
$emps[0], $emps[1], $emps[2] <br>
";
}




error:
%% [Sun Aug 13 14:00:45 2006] GET /cgi-bin/viewguest.cgi HTTP/1.1
%% 500 /big/dom/xprimehomesrealestate/cgi-bin/viewguest.cgi
%request
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding: gzip,deflate
Accept-Language: en-us,en;q=0.5
Connection: keep-alive
Host: www.primehomesrealestate.com
Keep-Alive: 300
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7
%response
%stderr
Can't locate object method "connect" via package "Mysql" (perhaps you forgot to load "Mysql"?) at viewguest.cgi line 11.