为什么php PDO登录不起作用?

密钥K

我试图用PDO登录,但我不能。当我尝试使用旧数据库连接时,它可以工作,但使用PDO时,它却不能工作;我无法确定问题出在哪里,连接正常,但代码无法导航到其他页面,电子邮件和密码正确。请查看下面的邮件,如果可以帮助我。

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

try {
    $conn = new PDO("mysql:host=$servername;dbname=social", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

class UserLogin {

    public $id;
    public $first;
    public $last;
    public $email;
    public $password;

    function Login(){
        global $conn;
        $this->email = $_POST['user_email'];
        $this->password = $_POST['user_email'];
        $sql = $conn->prepare("SELECT * FROM scusers WHERE scemail =? AND scpass =?");
        $sql->execute(array($this->email, $this->password));
        while($row = $sql->fetch(PDO::FETCH_ASSOC)){
            $this->email = $row['scemail'];
            $this->password = $row['scpass'];
        }

    }

}

<?php
//print_r($_POST);
if(isset($_POST['user_email']) && isset($_POST['user_password'])){
    $login = new UserLogin();
    $login->email = $_POST['user_email'];
    $login->password = $_POST['user_password'];
    if($login->Login()){
        header("Location:home.php");
    }else{
        echo "Negative";
    }
}
?>
  <body>
<header>

            <form action="#" method="post">
                <div class="col-sm-6">
                    <div class="row">
                        <div class="col-sm-5">
                            <div class="form-group">
                                <input name="user_email" type="text" class="form-control" placeholder="Email Address">
                                <div class="login-bottom-text checkbox">
                                    <label>
                                        <input type="checkbox" id="">

                                    </label>
                                </div>
                            </div>
                        </div>  
                        <div class="col-sm-5">
                            <div class="form-group">
                                <input name="user_password" type="text" class="form-control" placeholder="Password">
                                <div class="login-bottom-text"><a class="bottom-txt" href="recover_password.php">Forgot your password?</a></div>
                            </div>
                        </div>
                        <div class="col-sm-2">
                            <div class="form-group">
                                <button type="submit" value=" Send" class="btn btn-default" id="submit">login</button>
                            </div>
                        </div>
                    </div>  
                </div>
            </form>
u_mulder

Login()方法没有返回语句。没有return语句的函数返回NULLNULL被认为是虚假的因此,更改Login()方法至少会以这种方式返回一些真实值:

function Login(){
    $user_found = false;

    global $conn;
    $this->email = $_POST['user_email'];
    $this->password = $_POST['user_email'];   // is it?
    $sql = $conn->prepare("SELECT * FROM scusers WHERE scemail =? AND scpass =?");
    $sql->execute(array($this->email, $this->password));
    while ($row = $sql->fetch(PDO::FETCH_ASSOC)){
        $this->email = $row['scemail'];
        $this->password = $row['scpass'];

        $user_found = true;
    }

    return $user_found;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章