检查数据是否存在的函数返回false,而凭证是正确的

时间:2013-09-08 20:05:23

标签: php mysqli

由于某种原因,validatelogin返回false,而我确信凭据是正确的。我正在使用的语法有问题吗?提前谢谢!

function validateLogin($username, $password)
        {
            var_dump(func_get_args());
            if($stmt = $this->dbh->prepare("SELECT * FROM user WHERE username = ? AND password = ?"))
            {
                $stmt->bind_param('ss', $username, $password);
                $stmt->execute();
                if($stmt->num_rows > 0)
                {
                    $user = $stmt->fetch_assoc();
                    return $user;   
                }
                return false;

            }
        }

function login()
        {
            $this->username = $_POST['username'];
            $this->password = $_POST['password'];
            if(!empty($this->username) && !empty($this->password))
            {
                $user = $this->userModel->validateLogin($this->username, $this->password);
                var_dump($user); //FALSE    
                if($user){
                    $this->user = $user;
                    $_SESSION['user_id'] = $user['id'];
                    header('Location: http://localhost/cms/user.php?id=' . $_SESSION['user_id']); 
                }
                else
                {
                    echo "user not found";
                }
            }
            else
            {
                echo "not filled";
            }
        }

1 个答案:

答案 0 :(得分:0)

我认为你的函数返回false是因为你告诉它在if $stmt内返回fase,给它一个镜头:

function validateLogin($username, $password)
    {
        var_dump(func_get_args());
        if($stmt = $this->dbh->prepare("SELECT * FROM user WHERE username = ? AND password = ?"))
        {
            $stmt->bind_param('ss', $username, $password);
            $stmt->execute();
            if($stmt->num_rows > 0)
            {
                $user = $stmt->fetch_assoc();
                return $user;   
            }
             else{
                 return false; 
              } 

        }
        else{
             return false;
           }

       }