Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Avoid SQL injection

by Corion (Patriarch)
on Jan 20, 2023 at 11:49 UTC ( #11149722=note: print w/replies, xml ) Need Help??

in reply to Avoid SQL injection

Avoiding SQL injection is simple. Use DBI placeholders.

Instead of concatenating your values into your string, put ? there, and then place the values in ->execute(...).

You have structured your code around concatenating strings, so I would first replace the sqlSelect</p> with something like:

sub sqlSingleRow( $sql, @placeholders ) { my $sth = $dbh->prepare( $sql ); $sth->execute(@placeholders); # Do you really get only ever a single row back? my (@r)=$c->fetchrow(); $c->finish(); return @r; }

... and then use that routine like:

my $sql_cat = <<'SQL'; select ref_categorie from categorie_libelle_langue , libelle , langue from ref_categorie where libelle.libelle = ? AND categorie_libelle_langue.ref_libelle = libelle.id_libel +le AND categorie_libelle_langue.ref_langue = langue.id_langue +AND langue.key = ? SQL my ($cat) = sqlSingleRow($sql_cat, $category, $lang );

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2023-10-02 23:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found