Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi, I've the following code to start to paginate a search and want to not allow SQL injection
sub doSearch { my $article = $query->param ('search_name'); my $category = $query->param("categories"); my $string = ""; my $add; my $select = "DISTINCT article.nom"; my $from ; my $where; my $test = $SERVER{'all_categories'}; $category = trimwhitespace($category); $test = trimwhitespace($test); if ($category ne $test) { $add .= "AND article.ref_categorie = categorie_libelle_langue.ref +_categorie AND libelle.libelle = '$category' AND categorie_libelle_la +ngue.ref_libelle = libelle.id_libelle AND categorie_libelle_langue.re +f_langue = langue.id_langue AND langue.key = '$lang'"; $from .= "article,categorie_libelle_langue, libelle, langue"; }else { #$category = ''; $from .= "article"; } my $total = '0'; my $username = $query->param("username"); my ($cat)= $db->sqlSelect("ref_categorie", "categorie_libelle_lan +gue, libelle, langue", "libelle.libelle = '$category' AND categorie_l +ibelle_langue.ref_libelle = libelle.id_libelle AND categorie_libelle_ +langue.ref_langue = langue.id_langue AND langue.key = '$lang'"); my ($userID)= $db->sqlSelect("id_personne", "personne", "nom_util +isateur = '$username'"); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtim +e(); my $date = sprintf "%4d-%02d-%02d",$year+1900,$mon+1,$mday; my ($d)= $db->sqlSelect("count(article.nom)", $from, "article.nom + like '%$article%' AND ref_statut = '3' $add"); my $total = $d;
I want to know how is it safe to do sqlSelect queries within this code
</sub sqlSelect { my $from = shift || ''; my $select = shift || ''; my $where = shift || ''; my $other = shift || ''; my $other2 = shift || ''; my $sql="SELECT $select "; $sql.="FROM $where "; $sql.="WHERE $other "; my ($c)=$dbh->prepare($sql) or die "Sql has gone to hell\n"; #print "Content-Type: text/html\n\n"; #print "SQL : $sql \n"; if(not ($c->execute())) { my $err=$dbh->errstr; return undef; } my (@r)=$c->fetchrow(); $c->finish(); return @r; }
Thanks for your advice

In reply to Avoid SQL injection by *alexandre*

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 drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found