当前位置:Gxlcms > PHP教程 > mysql-关于php函数addslashes的一些小疑问

mysql-关于php函数addslashes的一些小疑问

时间:2021-07-01 10:21:17 帮助过:5人阅读

我把用户通过post发过来的数据"my'name"通过addslashes函数进行过滤,然后存入mysql。
但是当我在php脚本页面执行
$conn = new mysqli('127.0.0.1','root','','book_sc');
$re = $conn->query("select * from sc where title='"."my'name"."'");
var_dump($re->num_rows); //返回是NULL

但是在mysql里执行命令
mysql> select * from sc where title="my'name";
或者
mysql> select * from sc where title="my\'name";
都可以查询到这条数据,那为什么在php脚本页面执行就返回null呢?
还有就是过滤的函数还有strip_tags、htmlspespecialchars分别用于过滤掉html、php代码和转义html代码以保证数据的安全性,那我是不是需要把这3个函数都用上?

回复内容:

我把用户通过post发过来的数据"my'name"通过addslashes函数进行过滤,然后存入mysql。
但是当我在php脚本页面执行
$conn = new mysqli('127.0.0.1','root','','book_sc');
$re = $conn->query("select * from sc where title='"."my'name"."'");
var_dump($re->num_rows); //返回是NULL

但是在mysql里执行命令
mysql> select * from sc where title="my'name";
或者
mysql> select * from sc where title="my\'name";
都可以查询到这条数据,那为什么在php脚本页面执行就返回null呢?
还有就是过滤的函数还有strip_tags、htmlspespecialchars分别用于过滤掉html、php代码和转义html代码以保证数据的安全性,那我是不是需要把这3个函数都用上?

我很奇怪你执行 $re = $conn->query("select * from sc where title='"."my'name"."'");
怎么没有报错? 很明显这sql就有错误。
应该这样 $conn->query("select * from sc where title='my\'name'")

人气教程排行