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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.