gpt4 book ai didi

php - PDO 获取与行计数

转载 作者:行者123 更新时间:2023-11-29 18:28:05 24 4
gpt4 key购买 nike

我正在尝试使用 PDO 在 PHP 中创建登录脚本,但遇到了一个小问题。我可以检查数据是否在字段中,但不确定如何检查用户名和密码组合是否正确。这是检查用户名和密码的片段:

# if data is valid, check against values held in the database
if ($valid) {
$sql = 'SELECT COUNT(*) FROM authors
WHERE email = :email AND password = :password';

$s = $dbConnection->prepare($sql);
$s->bindValue(':email', $email);
$s->bindValue(':password', $password);
$s->execute();

$row = $s->fetch();

if($row==1){
echo "ok";
}else{
echo "no";
}
}

当我输入用户名和密码的任意组合时,我得到的输出为 false(即 echo“no”)。有人可以帮我吗?

<小时/>

更新:已修改代码段

我使用了password_verify,但结果仍然相同:

// if data is valid, check against values held in the database
if ($valid) {
$sql = 'SELECT * FROM author
WHERE email = :email';

$s = $dbConnection->prepare($sql);
$s->bindValue(':email', $email);
$s->execute();

$row = $s->fetch();
var_dump($row);

if(count($row) > 0 && password_verify($password, $row['password'])){
echo 'ok';
exit;
}else {
echo 'Username and Password are not found';
}
}

错误数据的var_dump:

bool(false) Username and Password are not found

正确数据的var_dump:

array(4) { ["id"]=> string(1) "3" ["name"]=> string(7) "a@a.com" ["email"]=> string(7) "a@a.com" ["password"]=> string(1) "a" } Username and Password are not found

最佳答案

已经解决了。我使用的解决方案是:

# if data is valid, check against values held in the database and then action as appropriate
if ($valid) {
$sql = 'SELECT id, email, password FROM author
WHERE email = :email AND password = :password';

$s = $dbConnection->prepare($sql);
$s->bindValue(':email', $email);
$s->bindValue(':password', $password);
$s->execute();

$data = $s->fetch();
if($data != NULL){
$_SESSION['loggedIn'] = TRUE;
$_SESSION['email'] = $email;
$_SESSION['id'] = $data['id'];
header('Location:../admin/');
exit;
}else{
unset($_SESSION['loggedIn']);
unset($_SESSION['email']);
unset($_SESSION['id']);
echo 'Incorrect username/password combination';
}
}

关于php - PDO 获取与行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45985267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com