php - SQL中条件字段和表字段名相同,造成全表查询
巴扎黑
巴扎黑 2017-04-11 09:58:31
0
7
178

各位大神好,在下是小菜鸟一枚,在实践中发现,类似如下的语句:

SELECT * FROM seller_item_classify where sid=$sid order by cweight asc ;

其中$sid为前端传过来的数值,seller_item_classify为表明,sid为表中的一个字段名;
如果$sid传过来的值正好是'sid'的时候,SQL的这个where就失效了,造成了全表查询;

因为在生产环境中,$sid的值可能是数值,可能是char;我应该在php里做对前端输入值的过滤?

请问各位是如何看这个问题?

巴扎黑
巴扎黑

reply all (7)
Ty80
"SELECT * FROM seller_item_classify where sid='$sid' order by cweight asc ;"
    刘奇

    SQL的条件值都加''

    sid = '$sid'
      黄舟

      对于前端输入值,后端必须过滤,建议使用sql预处理。

        PHPzhong

        as 别名好使吗?

          洪涛

          加上单引号就解决了

            巴扎黑

            'SELECT * FROM seller_item_classify where sid='.$sid.' order by cweight asc ;'
            用pdo预处理吧

              PHPzhong
                Latest Downloads
                More>
                Web Effects
                Website Source Code
                Website Materials
                Front End Template
                About us Disclaimer Sitemap
                php.cn:Public welfare online PHP training,Help PHP learners grow quickly!