in reply to Problem with DBI and MySQL
That said, placeholders do not necessarily have a place in once-off queries. What you absolutely MUST do when placing your data inline like this, is quote your data using the $dbh->quote() method. Make it a habit, even when you trust your variables, because you may reuse the code later.
Not only does ->quote() place quotation marks around your strings (failure to do so will make the query fail) but it will also escape potentially harmful characters that could be injected by a hacker.
This script won't insert anything, but if you are logged into MySQL with sufficient privileges it will ruin your day. Protecting yourself is easy:my $name = "; DROP DATABASE mysql;"; my $sth = $dbh->prepare("INSERT INTO names (name) values ('$name')"); $sth->execute;
Now the evil name will be inserted into the table as expected.my $name = "; DROP DATABASE mysql;"; my $sth = $dbh->prepare("INSERT INTO names (name) values (".$dbh->quot +e($name).")"); $sth->execute;
Google "SQL Injection".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Problem with DBI and MySQL
by roboticus (Chancellor) on Aug 23, 2010 at 11:12 UTC | |
by FloydATC (Deacon) on Aug 25, 2010 at 15:52 UTC |