我已经使用准备好的插入语句好几年了,并假设它正确绑定了参数,否则会给出错误,但它似乎不像以下 php 绑定并插入记录而没有任何错误,而是将应该是 int 的字符串更改为零。因此,它可能可以防止 SQL 注入攻击,但最终会在表中得到虚假记录。例如。:
$q = "INSERT INTO `table` (id1, id2) VALUES (?, ?)";
$stmt = mysqli_stmt_init($dbc);
$stmt->prepare($q);
$id1 = 'aaaaaaa';
$id2= 'aaaaaaa';
$result = $stmt->bind_param('ii', $id1, $id2);
echo '<p>' . $result . '</p>'; // Trying to bind a string to an int returns true!
echo $dbc->error; // nothing
$stmt->execute(); // inserts record changing $id2 to zero but auto-increments primary key $id1
echo $dbc->error; // nothing
它在 Apache/2.2.14、PHP/5.3.1 和 MySQL 5.1.41 的 Xampp 环境中运行。谁能告诉我发生了什么事吗?
$stmt->bind_param() 不会检查给定变量的特定类型,它只会将它们转换为指定的类型。你的字符串'aaaaaaa'被转换成一个int值:0。这就是php的做法。
如果变量包含有用/正确的值,则数据库插入语句是错误的检查位置。之前就这样做,并且只有在验证有效的情况下才尝试插入它们。
要对 int 进行验证,您可以使用 php 函数 is_numeric() 或 is_int()。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)