Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP&;mySQLi:在使用prepare语句时,我还需要检查用户输入吗?_Php_Database_Mysqli_Sql Injection_Prepared Statement - Fatal编程技术网

PHP&;mySQLi:在使用prepare语句时,我还需要检查用户输入吗?

PHP&;mySQLi:在使用prepare语句时,我还需要检查用户输入吗?,php,database,mysqli,sql-injection,prepared-statement,Php,Database,Mysqli,Sql Injection,Prepared Statement,如果我在mySQLi中使用prepare语句,我仍然需要以任何方式转义或检查用户输入。例如,如果我有代码: $members = new mysqli("localhost", "user", "pass", "members"); $r_email = $_POST['r_email']; $check = $members->prepare("select user_id from users where email = ?"); $check->bind_param('s',

如果我在mySQLi中使用prepare语句,我仍然需要以任何方式转义或检查用户输入。例如,如果我有代码:

$members = new mysqli("localhost", "user", "pass", "members");
$r_email = $_POST['r_email'];
$check = $members->prepare("select user_id from users where email = ?");
$check->bind_param('s', $r_email);
$check->execute();
$check->store_result();
if ($check->num_rows > 0) {
    echo "user already registered";
    $check->close();
}
$members->close();
我是否需要将
$r\u email=$\u POST['r\u email']
更改为
$r\u email=mysql\u real\u escape\u字符串($\u POST['r\u email'])


谢谢。

您不需要使用准备好的语句进行转义,但是验证用户输入仍然是一个好主意(例如,必填字段已填充,数值字段包含数值等)

如果要作为参数传递,则无需转义值。事实上,您不应该这样做,因为您将在数据中插入文字反斜杠

这些必须是准备时SQL查询的一部分,因此RDBMS可以解析和验证它们。或在表单上验证用户输入


总是将SQL转义数据放入SQL语句中。永远不要在SQL语句之外转义SQL数据。

无需转义输入,但不要略过验证,您仍然可能会出现逻辑错误,例如提交空电子邮件,或者其他一些副作用,这些副作用可能会再次困扰您


始终验证,让您在以后进行错误测试时保持理智,这是我的座右铭@least。

感谢您提供了一个简单易懂的答案,避免了我不得不发布类似的问题。