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


In reply to DBI query failing to execute by Lawliet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.