我正在尝试从用户会话中更新用户详细信息,我不断收到“致命错误:未捕获的错误:在bool中调用成员函数bind_param()的错误”,我认为这是指事实SQL语句未作为字符串读取,因此未插入数据库中。有人可以帮忙吗?新手。谢谢
// PHP
if (isset($_POST['update-btn'])) {
$username = $_POST['username'];
$email = $_POST['email'];
//validation
if (empty($username)) {
$errors['username'] = "Username required!";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors['email'] = "Email is invalid!";
}
if (count($errors) === 0) {
$sql = "UPDATE user SET username=?, email=? WHERE id=?";
$stmt = $db->prepare($sql);
$stmt->bind_param('ssi', $username, $email, $_SESSION['id']);
if ($stmt->execute()) {
$_SESSION['id'] = $user['id'];
$_SESSION['username'] = $username;
$_SESSION['email'] = $email;
$_SESSION['message'] = "Your account details have been updated";
exit();
} else {
$errors = "Detail could not be updated.";
}
}
}
// HTML
<form action="update.php" method="post"> .
<div class="form-group">
<div class="form-label-group">
<input type="text" name="username" id="username" value="<?
php echo $_SESSION['username'];?>" class="form-control"
placeholder="Name" autocomplete="off"
autofocus="autofocus" required="true">
<label for="username">Username</label>
</div>
<br>
<div class="form-label-group">
<input type="email" name="email" id="email" value="<?
php echo $_SESSION['email'];?>" class="form-
control" placeholder="Email"
autocomplete="off" autofocus="autofocus" required="true">
<label for="email">Email</label>
</div>
</div>
<input class="btn btn-primary btn-block" id="update-
btn" name="update-btn" type="submit"
value="Update">
</div>
</div>
</form>
我希望在数据库中更新用户信息。