
jackdesert
New User
Sep 17, 2009, 2:48 PM
Post #1 of 3
(6807 views)
|
INSERT fails, but die is not executed
|
Can't Post
|
|
Hi, I'm new to this forum. Getting started with perl and DBI. I have a script that works when I put reasonable data into it, but when I put in values that mysql knows are invalid (doens't satisfy the uniqueness quality I specified for the `priority` field, for example), then the INSERT doesn't happen at all. Fine. No big deal. I didn't do any error checking. However, the puzzling part is this: the die "SQL Error: $DBI::errstr\n"; command never prints in my browser window. As a matter of fact, nothing further gets printed to the browser window. That makes it a bit tricky to debug.. Is there something I need to do to get the die() function to work? By the way, I am running XAMPP for Linux on Ubuntu 9. Mozilla Firefox 3.0. Here is the form that the user submits:
<html> <head> <link rel="stylesheet" type="text/css" href="styles.css" /> </head> <body> <h1>Add a task to your day! </h1> <center>Note that you can leave some fields blank.</center><br /> <form action="/cgi-bin/add_to_database.cgi" method="post"> <table> <tr> <td> Groove <br /> <select name="groove"> <option value="A-Energy">A-Energy</option> <option value="B-Energy">B-Energy</option> <option value="R-ejuvenate">R-ejuvenate</option> </select> </td> <td> description <br /> <input type="text" name="description" size="57"/><br /> </td> <td> priority <br /> <input type="text" name="priority" size="1"/><br /> </td> </tr> </table> <table> <tr> <td> min_minutes <br /> <input type="text" name="min_minutes" size="2" /><br /> </td> <td> max_minutes <br /> <input type="text" name="max_minutes" size="2" /><br /> </td> <td> doors_open_time <br /> <input type="text" name="doors_open_time" size="5"/><br /> </td> <td> doors_close_time <br /> <input type="text" name="doors_close_time" size="5"/><br /> </td> <td> do_at_exactly_time <br /> <input type="text" name="do_at_exactly_time" size="5"/><br /> </td> <td> <input type="submit" value="Submit" /> </td> </tr> </table> </form> </body> </html> And here is the perl script that is processing it:
#!/opt/lampp/bin/perl # was #!/usr/bin/perl use CGI; # Create the CGI object my $query = new CGI; # Output the HTTP header print $query->header ( ); # Print the body just in case we need it print "<html> <head></head> <body>"; # Capture the form results my $category = $query->param("groove"); my $description = $query->param("description"); my $priority = $query->param("priority"); my $min_minutes = $query->param("min_minutes"); my $max_minutes = $query->param("max_minutes"); my $doors_open_time = $query->param("doors_open_time"); my $doors_close_time = $query->param("doors_close_time"); my $do_at_exactly_time = $query->param("do_at_exactly_time"); use DBI; my $dbh = DBI->connect('dbi:mysql:honor_thyself','root','') or die "Connection Error: $DBI::errstr\n"; my $sql = "INSERT INTO `honor_thyself`.`tasks` (`category`, `priority`, `min_minutes`, `max_minutes`, `doors_open_time`, `doors_close_time`, `do_at_exactly_time`, `description`) VALUES ('$category', '$priority', '$min_minutes', '$max_minutes', '$doors_open_time', '$doors_close_time','$do_at_exactly_time', '$description');"; #print "sql is $sql <br /><br />"; my $sth = $dbh->prepare($sql); print"sql successfully prepared <br /><br />"; my $result = $sth->execute or die "SQL Error: $DBI::errstr\n"; my $sql2 = "SELECT * from tasks"; my $sth2 = $dbh->prepare($sql2) or die "SQL Error: $DBI::errstr\n"; my $result2 = $sth2->execute or die "SQL Error: $DBI::errstr\n"; print"<h2>Here are all your tasks to choose from</h2>"; while (my @row2 = $sth2->fetchrow_array) { print "@row2 <br /> <br />"; } # Thank the user print "Thanks for filling in our form!</body></html>";
|