我的SQL语句有问题,它在第一个if语句上给出错误,该SQL语句已失败。对于准备好的语句,我使用了mmtuts和w3schools,但我不知道出了什么问题。
这是我的代码:
function login($conn) {
if (isset($_POST['submitLogin'])) {
$username = $conn->escape_string($_POST['emailorusername']);
$password = $conn->escape_string($_POST['password']);
$sql = "SELECT * FROM users WHERE username = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ?error=sqlstatementfailed");
exit();
} else {
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "s", $username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
if (mysqli_num_rows($result) == 0) {
header("Location: ?username=notfound");
exit();
} else {
if (mysqli_num_rows($result) > 1) {
header("Location: ?error=toomuchresults");
exit();
} else {
header("Location: ?username=ok");
if ($password !== $row['password']) {
header("Location: ?password=false");
exit();
} else {
header("Location: ?password=ok");
$_SESSION['username'] = $row['username'];
$_SESSION['email'] = $row['email'];
$_SESSION['firstname'] = $row['firstname'];
$_SESSION['lastname'] = $row['lastname'];
header("Location: Index");
exit();
}
}
}
}
}
}
服务器已连接到数据库,所以这不是问题。
有人可以帮忙吗?谢谢!
解决方案:
准备完成后检查mysqli errors,它将告诉您失败的原因。