CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: DBI:
INSERT fails, but die is not executed

 



jackdesert
New User

Sep 17, 2009, 2:48 PM

Post #1 of 3 (4765 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

Code
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:

Code
<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:

Code
#!/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>";



1arryb
User

Sep 18, 2009, 9:09 AM

Post #2 of 3 (4756 views)
Re: [jackdesert] INSERT fails, but die is not executed [In reply to] Can't Post

Hi jackdesert,

I don't do much CGI programming, but I know that messages printed to STDERR won't necessarily get back to your browser window. Try sending your error messages to a log file instead and see what you get.

Cheers,

Larry


jackdesert
New User

Sep 18, 2009, 10:01 AM

Post #3 of 3 (4750 views)
Re: [1arryb] INSERT fails, but die is not executed [In reply to] Can't Post

Okay, I got a working error message by making my own function

Code
sub die_openly 
{
my $message = shift;
print "<h2>$message</h2>\n";
die;
}

So now instead of saying

Code
or die "jibberish";

I just say

Code
or die_openly('jibberish');

and it spits out a big, beautiful error message. at some point I'll get fancy and want to redirect to another page, but this is enough for now. Thanks for the response..
-Jack

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives