Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm brand new to Perl and have tried researching how to submit data from a form into my MySQL database using Perl (.pl file) and can't get it to work. I created the form from a form creation tool of my web host, and it works when just sending the information in an email (and not the db). Now I'm trying to upload simple .pl files to my cgi-bin just to see if they would work to insert a simple row of data, and I can't even get those to work (get the internal server error). I have no idea what I'm doing wrong and have read through hundreds of examples with no luck. Here is a simple .pl file that I was trying to use just to see if it would work (I was trying to use a direct link to the .pl file, can you do that? e.g. www.sitename.com/cgi-bin/file.pl); can anyone help?? I'm very frustrated:

# HelloMySQL.pl # # MySQL STARTS HERE use strict; use CGI qw(:standard); use DBI; #MySQL CONFIG VARIABLES $host = "ftp here"; $database = "dbname here"; $tablename = "tablename here"; $user = "userid here"; $pw = "pw here"; # CONNECT TO DB $db = DBI->connect($host, $database, $user, $pw); # SELECT DB $db->selectdb($database); # DEFINE A MySQL QUERY $myquery = "INSERT INTO $tablename (fname, lname, street, city, state, zip, emai +l, age) VALUES ('fname1,'lname1','street1', 'city1', 'state1', ' +zip1', 'email1', 'age1')"; # EXECUTE THE QUERY FUNCTION $execute = $connect->query($myquery); $execute->execute(); # CLOSE DB CONNECTION $db->disconnect(); exit;

Replies are listed 'Best First'.
Re: Need help connecting to MySQL from .pl
by CountZero (Bishop) on Mar 09, 2008 at 23:04 UTC
    Wow, I hardly know where to start ...

    Perhaps you could begin by putting your code in <code> ... </code> tags. It looks much more nicely.

    Next, you use strict; which is good, but then none of your variables are declared with my so your code will not even run. Did you try this code at home on your own computer before uploading it? If you did you would have seen it didn't work.

    You execute the SQL through a $connect object variable but your never define it anywhere.

    I would suggest you start with something a bit less difficult, learn about the basics of Perl and study the DBI-module.

    I have also the feeling your connect call is wrong. The syntax is $dbh = DBI->connect($data_source, $username, $password) or die $DBI::errstr; and $data_source should be in the dbi:DriverName:database_name format/

    More specifically for MySQL it should be:

    my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port"; my $dbh = DBI->connect($dsn, $user, $password);

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Need help connecting to MySQL from .pl
by ysth (Canon) on Mar 10, 2008 at 00:38 UTC
    Your "internal server error" means your code had an error that probably ended up in your webserver's error log. Having access to the error log is really mandatory for doing web development, but in a pinch you can put a use CGI::Carp qw(fatalsToBrowser); at the very top of your script and see some errors in the browser.
Re: Need help connecting to MySQL from .pl
by apl (Monsignor) on Mar 10, 2008 at 09:54 UTC
    On top of everything else you've been told, you really should test (and display) any error returned.