Heim  >  Artikel  >  Backend-Entwicklung  >  SQL中条件字段和表字段名相同,造成全表查询

SQL中条件字段和表字段名相同,造成全表查询

WBOY
WBOYOriginal
2016-08-18 09:15:291418Durchsuche

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

<code>SELECT * FROM seller_item_classify where sid=$sid order by cweight asc ;
</code>

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

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

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

回复内容:

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

<code>SELECT * FROM seller_item_classify where sid=$sid order by cweight asc ;
</code>

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

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

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

<code>"SELECT * FROM seller_item_classify where sid='$sid' order by cweight asc ;"</code>

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

SQL的条件值都加''

<code>sid = '$sid'</code>

as 别名 好使吗?

加上单引号就解决了

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

<code><?php $sid = issset($_REQUEST['sid']) ?  htmlspecialchars(trim($_REQUEST['sid'])) : '';
if (!$sid or $sid=='sid') {
    // 非法请求 这里可以抛出一些异常
}</code></code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn