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
-
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.