几年来我一直在使用准备好的插入语句,并假设它正确绑定参数或会给出错误,但它似乎不是因为以下 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 环境中运行的。谁能告诉我发生了什么?