因此,我从TutorialRepublic复制了一份注册表单和登录表单,当该表单不起作用时,我添加了一些警报以找出未在运行哪些代码。
我最终发现mysqli_prepare
部分已被完全跳过:
$sql = "SELECT id, username, password FROM acccounts WHERE username = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);
echo "<script type='text/javascript'>alert('yes');</script>";
// Set parameters
$param_username = $username;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Store result
mysqli_stmt_store_result($stmt);
// Check if username exists, if yes then verify password
if(mysqli_stmt_num_rows($stmt) == 1){
// Bind result variables
mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
if(mysqli_stmt_fetch($stmt)){
if(password_verify($password, $hashed_password)){
// Password is correct, so start a new session
session_start();
// Store data in session variables
$_SESSION["loggedin"] = true;
$_SESSION["id"] = $id;
$_SESSION["username"] = $username;
// Redirect user to welcome page
header("location: index.php");
} else{
// Display an error message if password is not valid
$password_err = "The password you entered was not valid.";
}
}
} else{
// Display an error message if username doesn't exist
$username_err = "No account found with that username.";
}
// Close statement
mysqli_stmt_close($stmt);
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
else{
echo "<script type='text/javascript'>alert('error');</script>";
}
我知道最好只是带别人的教程代码,然后逐字使用它,但是我真的只是需要这样做。我对PHP和MySQL还是很陌生,所以我什至不知道从哪里开始尝试调试。
答案 0 :(得分:0)
好的,所以本质上我只需要调试更好。在两个用户的评论帮助下,我能够找到错误所在。
我已经做出else
的声明,警告说mysqli_prepare
已被跳过。在该else
语句中,我应该添加echo mysqli_error ($link);
,因为这将输出错误信息。