【发布时间】:2022-01-23 23:31:40
【问题描述】:
我被难住了。我只需要有人查看我的代码并就连接不工作的原因给我一些想法。它适用于我拥有的所有其他更新功能(类似的代码/复制粘贴),但不是这个。
我还必须补充一点,它运行并更新数据库。我的问题是最后得到结果。我将错误追溯到连接,它已经没有任何意义了。
/**
* Update Avatar Function
*/
function updateAvatar($conn, $username, $avatar) {
// setup a query
$sql = "UPDATE users SET avatar = ? WHERE username = ?;";
// prepare a statement to send a sanitized query
$stmt = mysqli_stmt_init($conn);
// check for a fail in case the query is faulty
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../admin/account.php?error=stmtfailed");
exit();
}
// bind our data to the statement and execute
mysqli_stmt_bind_param($stmt, "ss", $avatar, $username);
mysqli_stmt_execute($stmt);
// save the avatar
$avatar = json_decode($avatar, true);
move_uploaded_file($avatar["tmp_name"][0], '../assets/images/uploads/' . $avatar["name"][0]);
// grab the returned data
$resultData = mysqli_stmt_get_result($stmt);
// try to grab the row and return it
if (!$row = mysqli_fetch_assoc($resultData)) {
return $row;
}
// close the statement
mysqli_stmt_close($stmt);
// send the user to the account page with a success message
header("location: ../admin/account.php?success=update");
}
【问题讨论】:
-
您遇到的错误是什么?
-
我认为你应该从这个声明中删除
!,除非我误读了它:if (!$row = mysqli_fetch_assoc($resultData))。如果失败,或者没有返回结果,那么就没有什么可返回的了吗? -
@ChrisHaas 应该删除整个
if语句,因为它没有任何意义。我认为这是 OP 正在谈论的错误 -
是的,我现在正在阅读 SQL,我看到有一个
UPDATE。也许他们想要mysqli_stmt_affected_rows()? -
mysqli_stmt_get_result:
For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, mysqli_stmt_get_result() will return a mysqli_result object. For other successful queries, mysqli_stmt_get_result() will return false.因此$resultData将是错误的,mysqli_fetch_assoc($resultData)很可能会抛出错误,而不是做任何你认为你想让它做的事情