有人可以帮我修改我的代码吗? 我正在尝试访问密码哈希数据库并使用它们来验证用户登录,但是我收到了一些错误。
<?php
$servername="localhost";
$username = "*****";
$password = "*******";
$dbname = "*****";
$conn = new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
die("Connection to database failed: ".$conn->connect_error);
}
$uname=mysqli_real_escape_string($conn, $_POST['entered_username']);
$pw=mysqli_real_escape_string($conn, $_POST['entered_password']);
$stmt=$conn->prepare("SELECT username,password,password_hash FROM users WHERE username=?");
$stmt->bind_param('s',$uname);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);
$stmt->fetch();
if(!$stmt){
echo $conn->connect_error();}
if($stmt){
echo 'Connection successful';}
$found=FALSE;
while($row=mysqli_fetch_assoc($stmt))
{
if($password_verify($pw,$row['password_hash'])) {
$found=TRUE;
}
}
if($found){
echo "You have successfully logged in as ".$uname."!";
}
else {
echo "Login as ".$uname." failed!";
}
$stmt->close();
$conn->close();
?>
我得到的是输出:
警告:mysqli_stmt :: bind_result():绑定变量数没有 匹配预备语句中的字段数 第27行///login3.php
连接成功
警告:mysqli_fetch_assoc()期望参数1为mysqli_result, 第37行的/****/login3.php中给出的对象
以管理员身份登录失败!
答案 0 :(得分:0)
你正在混淆它。试试 -
$stmt=$conn->prepare("SELECT username,password,password_hash FROM users WHERE username=?");
$stmt->bind_param('s',$uname);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);
if(!$stmt){
echo $conn->connect_error();}
if($stmt){
echo 'Connection successful';}
$found=FALSE;
while($row=$stmt->fetch_assoc();)
{
if($password_verify($pw,$row['password_hash'])) {
$found=TRUE;
}
}
答案 1 :(得分:0)
你必须绑定列,因为它必须匹配需要的字段数:
检查一下:
<?php
$servername="localhost";
$username = "*****";
$password = "*******";
$dbname = "*****";
$conn = new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
die("Connection to database failed: ".$conn->connect_error);
}
$uname=mysqli_real_escape_string($conn, $_POST['entered_username']);
$pw=mysqli_real_escape_string($conn, $_POST['entered_password']);
$stmt=$conn->prepare("SELECT username,password,password_hash FROM users WHERE username=?");
$stmt->bind_param('s',$uname);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($username,$password,$password_hash);
$stmt->fetch();
if(!$stmt){
echo $conn->connect_error();}
if($stmt){
echo 'Connection successful';}
$found=FALSE;
while($row=mysqli_fetch_assoc($stmt))
{
if($password_verify($pw,$password_hash)) {
$found=TRUE;
}
}
if($found){
echo "You have successfully logged in as ".$uname."!";
}
else {
echo "Login as ".$uname." failed!";
}
$stmt->close();
$conn->close();
?>
答案 2 :(得分:0)
谢谢你们!它现在有效!我更改了bind_result语句并删除了fetch语句。显然,$stmt is of type mysqli_stmt, not mysqli_result和mysqli_stmt类没有为它定义fetch_assoc()方法。
$stmt=$conn->prepare("SELECT username,password_hash FROM users WHERE username=?");
$stmt->bind_param('s',$uname);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($user,$password_hash);
$found=FALSE;
while($stmt->fetch())
{
if(password_verify($pw,$password_hash)) {
$found=TRUE;
}
}