Click to See Complete Forum and Search --> : Perl>DBI>mySQL


Bungholio
09-08-2003, 09:24 PM
Hey ppl, thnx for looking!

I just wanted to throw in a little DB storage of Name/Email into a already functioning PERL script, that i did not write. This is my first venture into Perl, so sorry if its something st00pid. I tried looking at the dbi.perl.com stuff but the links are either dead or go back to perl.com which tell you to go to dbi.perl.com ... ugh ... anyways, i just added this in the script;

note: the top bit above 'My New Stuff' comment is the top of the file.



#!/usr/bin/perl

# Get the data
%input = e"t_request;

#---->My New Stuff<---
use DBI;

my $serverName = "externalsite.com";
my $serverUser = "userName";
my $serverPass = "passWord";
my $serverDb = "dbName";
my $serverTabl = "tableName";

$dbh = DBI->connect("DBI:mysql:database=$server Db;host=$serverName;",$serverUser,$serverPass);
$Name = $input{'name'};
$Email = $input{'from_email'};
$myDate = '1900-00-00 24:00:00';
$success = $dbh->do("INSERT INTO $serverTabl(Name,Email,Date) VALUES(?,?,?)", undef, $Name, $Email, $myDate);
$dbh->disconnect;
#------------------------------------------



Ive double checked the capitization and spelling, and the date is bogus, the name and email come from the previous page...

anwyays, i just get a 500 internal server error, and the host will not help with MY script :( :( :( ... so if you can help at all, i would really appreciate it,

thanks again,
Al

GaijinPunch
09-08-2003, 10:08 PM
Maybe I can provide a few tips.

First, if you want to see some errors (instead of the very vague 500) try this:

use CGI:Carp qw(fatalsToBrowser);
That will return any PERL errors. I'm not 100% sure, but I think you'll need to declare a CGI object to get the errors though. (Do a google search for CGI.pm)

There's also a way to dump SQL errors from the browser which I found here:
http://forums.devshed.com/t38972/s.html
I believe in your case, it would be:
$dbh->execute() or die $dbh->errstr;
I'm pretty sure you need the fatalsToBrowser thing set at the top of your Perl script for the MySQL errors to show up in the browser.

IMPORTANT: Don't leave fatalsToBrowser on after your site is debugged. It can give some very good info about your site (and provider) if you're not careful. Means it's easier to get hacked.

Okay, so more specifically w/ your problem.

First:
%input = e"t_request;
-- I think you must've typed that wrong. Perl would choke on that.

Second:
Something MySQL will choke on is your quotes.
$success = $dbh->do("INSERT INTO $serverTabl(Name,Email,Date) VALUES(?,?,?)", undef, $Name, $Email, $myDate);

each of your variables that actually holds a string (or a date, or something like that) should look like \"$variable\";
In other words, MySQL needs the quotes.
Easy query example:

SELECT * FROM myTable WHERE title = "MyTitle";

If you don't have the quotes around "MyTitle", MySQL doesn't know to parse it as a string -- that's what you're doing in your script. You're inserting values that need quotes, w/o the quotes.

Also - the "\" makes Perl use the quote as a string, not as a "real" quote. This might seem confusing, but is pretty standard in any Unix shell, or scripting language.

Finally - I think your insert command just might be wrong:
1: Why the 'undef' ?
2: Why not use \"$Name",\"$Email\",\"$myDate\" instead of (?,?,?) ? I'm pretty new to MySQL myself, so am definitely not the best person to comment.

Lastly (I know this is long, but it's worth it)
You should execute perl like this:
#!/usr/bin/perl -wT
the -w gives you warnings (this is for use even w/ easy shell scripts, not just CGI). The -T is something you use for CGI, but I can't for the life of me remember why. :(
When you use -w, it's going to tell you that "$success" needs to be declared, so change it to my $success = blahblahlbah. For the most part, it's hard to get help on Perl stuff inless you use warnings and strictures.