我有一个php用户类,可以添加,删除,更新用户。在更新功能上,它不返回任何错误,它返回成功,但实际数据库值未更改。
有什么想法吗?
这是我的user.class.php文件。
public function userUpdate($id, $fname, $lname) {
$pdo = $this->pdo;
if (isset($id) && !empty($id) && isset($fname) && !empty($fname) && isset($lname) && !empty($lname)) {
$stmt = $pdo->prepare('UPDATE users SET fname = ?, lname = ? WHERE id = ?');
if ($stmt->execute([$id, $fname, $lname])) {
$this->msg = 'User name was successfully updated!';
return true;
}
else
{
$this->msg = 'User name failed to update!';
return false;
}
}
else
{
$this->msg = 'Data lost or not set!';
return false;
}
}
这是我的update.func.php文件。
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['uid']) && !empty($_POST['uid']) &&
isset($_POST['fname']) && !empty($_POST['fname']) && isset($_POST['lname']) && !empty($_POST['lname'])) {
$id = filter_input(INPUT_POST, 'uid', FILTER_DEFAULT);
$fname = filter_input(INPUT_POST, 'fname', FILTER_SANITIZE_STRING);
$lname = filter_input(INPUT_POST, 'lname', FILTER_SANITIZE_STRING);
if ($user->userUpdate($id, $fname, $lname)) {
header('Location: https://domain/update.php?result=pass&message=' . $user->getMsg());
exit;
}
else
{
header('Location: https://domain/update.php?result=fail&message=' . $user->getMsg());
exit;
}
}
else
{
header('Location: https://domain/update.php?result=fail&message=' . $user->getMsg());
}
}
else
{
header('Location: https://domain/update.php?result=fail&message=' . $user->getMsg());
}
答案 0 :(得分:1)
您在执行以下命令时传递的参数顺序错误:
$stmt = $pdo->prepare('UPDATE users SET fname = ?, lname = ? WHERE id = ?');
if ($stmt->execute([$id, $fname, $lname])) {
您需要将它们传递为:
$stmt = $pdo->prepare('UPDATE users SET fname = ?, lname = ? WHERE id = ?');
if ($stmt->execute([$fname, $lname, $id])) {