0

出于某种原因 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";
            }
        }
4

1 回答 1

0

我认为您的函数返回 false 是因为您告诉它在 中返回 fase if $stmt,试一试:

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;
           }

       }
于 2013-09-08T22:13:02.500 回答