Lawliet has asked for the wisdom of the Perl Monks concerning the following question:
I'll try to keep this short and sweet. First, background information: I am writing a script that creates a CGI quiz (creates a .cgi file by printing the needed components) based on an info file telling the script where the questions are located, and how variables should be defined (such as if the user wants to implement a high score system using databases, and the credentials needed). So far so good.
I recently started to add the database function (the script works flawlessly if I set the $usedb variable to zero (I have it set up that all the print statements that print out anything related to databasing will be ignored if the $usedb is zero)). This, as one could easily guess, is where the problem lays. Examine the below code.
print CGI <<'EOF' if $usedb; sub store_to_db { my ($username, $score) = @_; my $dburl = "*withheld*"; my $dbname = "*withheld*"; my $dbtable = "*withheld*"; my $dbuser = "*withheld*"; my $dbpass = "*withheld*"; print $q->p("Invalid username or score below 20") if $username eq +'Anonymous' or $username eq '' or $score < 20; my $dbh = DBI->connect("DBI:mysql:$dbname:$dburl", "$dbuser", "$db +pass") or print $q->p("Could not connect"); my $sth = $dbh->prepare("INSERT INTO $dbtable (Username, Score) VA +LUES (?,?)") or print $q->p("Could not prepare"); $sth->execute($username, $score) or print $q->p("Could not execute +"); $sth->finish(); $dbh->disconnect; return 1; } EOF
When the cgi program that this script creates is run, it prints "Could not execute" to the screen, leading me to believe there is a problem executing the query. So, how do I fix this?
Update:Forgot to have the primary key auto-increment. Problem solved. Thanks NetWallah for helping me find the error.
I'm so adjective, I verb nouns!
chomp; # nom nom nom
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI query failing to execute
by NetWallah (Canon) on Aug 15, 2008 at 06:36 UTC | |
by Lawliet (Curate) on Aug 15, 2008 at 06:45 UTC |