PHP - mysqli_stmt_execute() 总是 returns 错误
PHP - mysqli_stmt_execute() always returns false
下午好,
我正在构建一个计算器,使用 JS 和 PHP,需要将结果和其他内容插入数据库。
我正在使用准备好的语句来实现这一点,但由于某种原因,我无法插入任何内容,因为 mysqli_stmt_execute() 总是返回 false。
$link = new_db_connection();
$link2 = new_db_connection();
$stmt = mysqli_stmt_init($link);
$stmt2 = mysqli_stmt_init($link2);
$query = "SELECT * FROM user_ip WHERE user_ip = '" . $myIP . "'";
$query2 = "INSERT INTO calc (time,operation,result,bonus,hash,fk_id_user_ip) VALUES (?,?,?,?,?,?)";
if (mysqli_stmt_prepare($stmt, $query)) {
mysqli_stmt_bind_result($stmt, $id_user, $user_ip);
if (mysqli_stmt_execute($stmt)) {
while (mysqli_stmt_fetch($stmt)) {
if (mysqli_stmt_prepare($stmt2, $query2)) {
mysqli_stmt_bind_param($stmt2, 'issisi', $timestamp, $operation, $result, $bonus_DB, $hash, $id_user);
if (mysqli_stmt_execute($stmt2)) {
mysqli_stmt_close($stmt);
mysqli_stmt_close($stmt2);
mysqli_close($link);
mysqli_close($link2);
}
}
}
} else {
mysqli_close($link);
}
}
我就是这样做的(如果 IP 不存在,还需要进行一些检查以添加 IP)。在 if (mysqli_stmt_execute($stmt2))
returns false 之前,它可以毫无问题地执行所有操作,因此它不会向数据库添加任何内容。
我是不是漏掉了什么?
编辑:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?,?,?,?,?,?)' at line 1
我写了这个:
if (!mysqli_query($link2, $query2)) {
echo("Error description: " . mysqli_error($link2));
}
执行前。
问题是您正在关闭循环中的语句和链接,因此您不能在循环重复时重用它们。
但是您不需要任何这种循环,它可以完全通过插入 SELECT
.
结果的一个查询来完成
$link = new_db_connection();
$stmt = $link->prepare("
INSERT INTO calc (time,operation,result,bonus,hash,fk_id_user_ip)
SELECT ?,?,?,?,?, id_user
FROM user_ip
WHERE user_ip = ?");
if (!$stmt) {
echo "Prepare error: " . mysqli_error($link);
exit;
}
$stmt->bind_param('ississ', $timestamp, $operation, $result, $bonus_DB, $hash, $myIP);
if (!$stmt->execute()) {
echo "Error description: " . mysqli_error($link);
}
$link->close();
下午好,
我正在构建一个计算器,使用 JS 和 PHP,需要将结果和其他内容插入数据库。
我正在使用准备好的语句来实现这一点,但由于某种原因,我无法插入任何内容,因为 mysqli_stmt_execute() 总是返回 false。
$link = new_db_connection();
$link2 = new_db_connection();
$stmt = mysqli_stmt_init($link);
$stmt2 = mysqli_stmt_init($link2);
$query = "SELECT * FROM user_ip WHERE user_ip = '" . $myIP . "'";
$query2 = "INSERT INTO calc (time,operation,result,bonus,hash,fk_id_user_ip) VALUES (?,?,?,?,?,?)";
if (mysqli_stmt_prepare($stmt, $query)) {
mysqli_stmt_bind_result($stmt, $id_user, $user_ip);
if (mysqli_stmt_execute($stmt)) {
while (mysqli_stmt_fetch($stmt)) {
if (mysqli_stmt_prepare($stmt2, $query2)) {
mysqli_stmt_bind_param($stmt2, 'issisi', $timestamp, $operation, $result, $bonus_DB, $hash, $id_user);
if (mysqli_stmt_execute($stmt2)) {
mysqli_stmt_close($stmt);
mysqli_stmt_close($stmt2);
mysqli_close($link);
mysqli_close($link2);
}
}
}
} else {
mysqli_close($link);
}
}
我就是这样做的(如果 IP 不存在,还需要进行一些检查以添加 IP)。在 if (mysqli_stmt_execute($stmt2))
returns false 之前,它可以毫无问题地执行所有操作,因此它不会向数据库添加任何内容。
我是不是漏掉了什么?
编辑:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?,?,?,?,?,?)' at line 1
我写了这个:
if (!mysqli_query($link2, $query2)) {
echo("Error description: " . mysqli_error($link2));
}
执行前。
问题是您正在关闭循环中的语句和链接,因此您不能在循环重复时重用它们。
但是您不需要任何这种循环,它可以完全通过插入 SELECT
.
$link = new_db_connection();
$stmt = $link->prepare("
INSERT INTO calc (time,operation,result,bonus,hash,fk_id_user_ip)
SELECT ?,?,?,?,?, id_user
FROM user_ip
WHERE user_ip = ?");
if (!$stmt) {
echo "Prepare error: " . mysqli_error($link);
exit;
}
$stmt->bind_param('ississ', $timestamp, $operation, $result, $bonus_DB, $hash, $myIP);
if (!$stmt->execute()) {
echo "Error description: " . mysqli_error($link);
}
$link->close();