各位大神好,在下是小菜鸟一枚,在实践中发现,类似如下的语句:
SELECT * FROM seller_item_classify where sid=$sid order by cweight asc ;
其中$sid为前端传过来的数值,seller_item_classify为表明,sid为表中的一个字段名;
如果$sid传过来的值正好是'sid'的时候,SQL的这个where就失效了,造成了全表查询;
因为在生产环境中,$sid的值可能是数值,可能是char;我应该在php里做对前端输入值的过滤?
请问各位是如何看这个问题?
'SELECT * FROM seller_item_classify where sid='.$sid.' order by cweight asc ;'
用pdo预处理吧
<?php
$sid = issset($_REQUEST['sid']) ? htmlspecialchars(trim($_REQUEST['sid'])) : '';
if (!$sid or $sid=='sid') {
// 非法请求 这里可以抛出一些异常
}
as 别名
好使吗?
"SELECT * FROM seller_item_classify where sid='$sid' order by cweight asc ;"
SQL的条件值都加''
sid = '$sid'
对于前端输入值,后端必须过滤,建议使用sql预处理。
加上单引号就解决了