使用PDO检查数据库凭据显示错误

时间:2018-01-18 04:06:01

标签: javascript php pdo

使用PDO创建登录和验证表单,这是显示正确运行并与localhost数据库连接的数据库的数据库代码。

<?php
$servername = "localhost";
$username = "root";
$password = "";

try {
    $con = new PDO("mysql:host=$servername;dbname=filesystem", $username, $password);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>   

现在我根据正确的用户名和密码从数据库验证后建立连接打开页面,这是代码

if(isset($_POST['submit']))
{
    $user=!empty($_POST['user']) ? trim($_POST['user']) :null ;
    $password=!empty($_POST['password']) ? trim($_POST['password']) :null;
    $sql="select * from users where user='$user' and password='$password'";
    if($stmt=$con->query( $sql))
    {
        if($stmt->fetchColumn()>0)
        {
            $row=$stmt->fetch(PDO::FETCH_ASSOC);
            $u_name=$row['user'];
            $u_password=$row['password'];
            if($user==$u_name && $password==$u_password)
            {
                if(isset($_POST['remember']))
                {
                    setcookie('user',$user,time()+60*60*7);
                    setcookie('password',$password,time()+60*60*7);
                }
                session_start();
                $_SESSION['user']=$user;
                echo "<script> window.location.assign('../index.php'); </script>"; 
                exit();        
            } else {
                echo "nothing";
            }
        } else {
            echo"<script>alert('incorrect user name or password')</script>";
            echo "<script> window.location.assign('login.php'); </script>"; 
        }
    }
}

此代码的输出显示else的{​​strong>输出,同时在此处检查来自表单和数据库的密码if($user==$u_name && $password==$u_password)并且输入凭据正确但仍显示此错误,一些身体请帮助我。

0 个答案:

没有答案