Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Insert into mysql database

by Tux (Canon)
on Mar 03, 2021 at 07:44 UTC ( [id://11129065]=note: print w/replies, xml ) Need Help??


in reply to Insert into mysql database

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129065]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found