不检查用户名和密码是否匹配

时间:2014-08-03 13:51:40

标签: php mysql sql

<?php

// Connecting to the database
$dbhandle = new PDO(
    'mysql:host=(hidden);dbname=thisismydb',
    'thisismydb',
    'database123'
    );

// Checking if the fields have been completed
$username = ($_POST['username']);
$password = ($_POST['password']);
$cryptpassword = crypt($password);

$sql="SELECT * FROM users WHERE username='$username' AND password='$cryptpassword'";
$query=$dbhandle->prepare($sql);
$query->execute();
$result=$query->fetchAll(PDO::FETCH_ASSOC);

if($result == true ){
    echo "You are logged in";
} else {
    echo "Your username and password do not match.";
}
?>

您好,

我正在尝试查看用户名和密码是否在数据库中匹配,并且我已经为此创建了此PHP代码以进行检查,但它似乎无法正常工作并且一直在说“用户”和密码不匹配&#39;。

我错过了什么吗?

由于

1 个答案:

答案 0 :(得分:1)

fetchAll没有返回truefalse,它会返回所有匹配行的数组。所以你应该做

$result = $query->fetchAll(PDO::FETCH_ASSOC);
if ($result === false) {
    die("Database error: " . print_r($dbhandle->errorInfo(), true));
}
if (count($result) > 0) {
    echo "You are logged in";
} else {
    echo "Your username and password do not match.";
}