Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Not really bringing new information into play, but just stressing what the others said: YOU ARE DOING IT WRONG!

The way you use SQL is extremely unsafe, and if you don't change that, you will hear somewhere in the near future "I told you so".

# prepare query my $query = <<"EOSQL"; INSERT INTO closedauctions ( seller, sellerusername, category, title, bids, pounds, description, winningbid, price_per_pound, highbidder, buyerusername, month, day, year, itemnum, filename) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) EOSQL my $sth = $dbh->prepare ($query) or die $sbh->errstr; # Execute query $sth->execute ( $sellername, $sellerusername, $quota, $category, 1, $pounds, $desc, $buyit, $price_per_pound, $buyername, $buyerusername, $month, $day, $year, $form{ITEM}, $cat ) or die $dbh->errstr; # Done inserting $sth->finish; # disconnect from database $dbh->disconnect;

Doesn't that look a lot easier to maintain? Besides that this is safer against attacks, it is also portable. e.g. your code would horribly fail if any of the data variable contans a ".

There is a subtle difference with your code: as you force quotation (which is unreliabvle to say the least) in your statement, undefined values will be stored as an empty string in the database (which should generate tons of warnings if you are running under use strict; and use warnings; (but I fear you are not). In this rewritten example, undefined values are stored as NULL.

One last thing to note that would help you find bugs, it to enable showing DBI errors when they happen:

my $dbh = DBI->connect ($dsn, $user, $pass, { RaiseError => 1, PrintError => 1, ShowErrorStatement => 1, });

Enjoy, Have FUN! H.Merijn

In reply to Re: Insert into mysql database by Tux
in thread Insert into mysql database by NorCal12

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found