因此,我从在线教程中获得了完整的代码,但是不幸的是,当他们提供代码时,有些地方我不知道要输入什么。我知道这可能是非常基本的,但对于不知道的人有关sql和php代码的内容很多,它们也将很难弄清基础知识。
很多人提出了难题,但从来没有像我这样的初学者难以理解的简单基本问题。
这里是a link!我从哪里获得信息。
我尝试过 google 并经历 stackoverflow 和 webdevtrick ,但是人们遇到的所有问题或疑虑都很难解决问题比我的基本问题还重要。
// Prepare a select statement
$sql = "SELECT id, username, password FROM users WHERE username = ?";
因此,我尝试将 1 , 0 放入,并将'username'放入问号区域,但没有运气。事与愿违的是糟糕!有些不对劲。请稍后再试。
验证凭据后的代码
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);
// 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: welcome.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.";
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
我敢打赌,它是如此基本,但是我希望,如果其他人也对此有困难,将会帮助他们并理解他们在该声明中应该写些什么。感谢您的帮助,如果您需要更多信息,请告诉我。
谢谢!
编辑要查看完整代码的人
// Initialize the session
session_start();
// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
header("location: welcome.php");
exit;
}
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Check if username is empty
if(empty(trim($_POST["username"]))){
$username_err = "Please enter username.";
} else{
$username = trim($_POST["username"]);
}
// Check if password is empty
if(empty(trim($_POST["password"]))){
$password_err = "Please enter your password.";
} else{
$password = trim($_POST["password"]);
}
// Validate credentials
if(empty($username_err) && empty($password_err)){
// Prepare a select statement
$sql = "SELECT id, username, password FROM users WHERE username = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Set parameters
$param_username = $username;
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_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: welcome.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.";
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>```
答案 0 :(得分:1)
您不会在问号旁边放置任何东西;问号是要。下一行mysqli_stmt_bind_param()
用编程语言$param_username
替换问号。
但是,在此语句中尝试使用它之前,您需要先为$param_username
分配一些东西:
$param_username = 'name';
$sql = "SELECT id, username, password FROM users WHERE username = ?";
$stmt = mysqli_prepare($link, $sql)
mysqli_stmt_bind_param($stmt, "s", $param_username);
mysqli_stmt_execute($stmt);
以上内容最终将查询SELECT id, username, password FROM users WHERE username = name
。
在上述(更新的)示例中,将$param_username
设置为$username
,将$username
设置为trim($_POST["username"])
。如果引荐页的name
中username
的{{1}}上没有<input>
的{{1}}字段,则该值为空。确保使用以下方法检查<form>
和$username
是否存在:
$password