我对 php / mysqli 比较陌生,我在验证用户时遇到问题,我不确定如何获取一行并验证数据,然后将用户重定向到新页面或显示错误(所有我知道我需要使用 _SESSION) 。基本上我在使用 fetch() 例程时遇到问题,编写这个代码的正确方法是什么?
<?php
session_start();
$username = trim($_POST['username']);
$password = trim($_POST['password']);
/* Create a new mysqli object with database connection parameters */
$mysqli = mysqli_connect('localhost', 'root', '', 'draftdb');
if(mysqli_connect_errno())
{
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
/* Create a prepared statement */
if($stmt = $mysqli -> prepare("SELECT Login_ID, Login_PW,
FROM login
WHERE Login_ID=? AND Login_PW =?"))
{
/* Bind parameters
s - string, b - boolean, i - int, etc */
$stmt -> bind_param("ss", $username, $password);
/* Execute it */
$stmt -> execute();
/* Bind results */
$stmt -> bind_result($username, $password);
/* Fetch the value */
if($stmt->fetch() == true)
{
$row =$query -> fetch();
$_SESSION['name'] = $row;
header("Location:/in.php");
exit();
}
else
{
echo 'Invalid Login';
}
/* Close statement */
$stmt -> close();
}
/* Close connection */
$mysqli -> close();
?>