. . . mentions the placeholder for an auto incre. id is not necessary (however, I have found in other scripts this was necessary--weird).

The node in question was correct. Not only is setting an auto increment field unnecessary, it's undesirable without a really good reason. I suspect the other scripts you mentioned were using a database with a bad schema.

$sth = $dbh->prepare (q{ INSERT INTO sponsor VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)} );

After you call prepare, add a or die "Couldn't prepare statement: " . $dbh->errstr;, or turn RaiseError on when you call DBI->connect (unless you already have). A similar note goes for the execute (which can fail if you have an incorrect number of parameters).

You have a huge number of placeholders there. Instead of a static SQL statement, you can generate the SQL at runtime to ensure you have the correct number of placeholders for your data. This can be done by pulling all the parameters you set out of execute statement and into a hash. The keys of the hash are the name of the fields, and the values are the data you put in for that field. Example:

my %sql_data = ( name => $name, address => $address, # and so on ); my $sql = q/INSERT INTO sponsor (/ . join(',', keys %sql_data) . q/) VALUES (/ . join(',', ('?') x keys %sql_data) . q/)/; my $sth = $dbh->prepare($sql) or die $dbh->errstr; # values() is guarenteed to come out in the same order as keys() $sth->execute(values %sql_data) or die $dbh->errstr; $sth->finish();

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated


In reply to Re: Using placeholders in MySQL returning an error by hardburn
in thread Using placeholders in MySQL returning an error by bradcathey

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.