警告:PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parame

Posted

技术标签:

【中文标题】警告:PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter is not defined in【英文标题】:Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in 【发布时间】:2018-05-31 05:12:12 【问题描述】:

代码如下:

<?php 
    class User 
         protected $pdo;enter code here

         function __construct($pdo)
            $this->pdo = $pdo;
         
    public function checkInput($var)
           $var = htmlspecialchars($var);
           $var = trim($var);
           $var = stripcslashes($var);
           return $var;
    

    public function login($email,$password)
        $stmt = $this->pdo->prepare("SELECT 'user_id' FROM 'users' WHERE 'email' = :email AND 'password' = :password");
        $stmt->bindParam(":user_id", $user_id, PDO::PARAM_STR);
        $stmt->bindParam(":password", $password, PDO::PARAM_STR);
        $stmt->execute();

        $user = $stmt->fetch(PDO::FETCH_OBJ);
        $count = $stmt->rowCount();

        if($count > 0)
            $_SESSION['user_id'] = $user->user_id;
            header('Location: home.php');
        else
            return false;
        
        
    
?>

【问题讨论】:

【参考方案1】:

您的查询中有语法错误,并且您试图传递错误的参数。您的$user_id 在哪里,所以它应该是$email,因为您正在接收$email$password

改变

$stmt->bindParam(":user_id", $user_id, PDO::PARAM_STR);

$stmt->bindParam(":email", $email, PDO::PARAM_STR);

改变

$stmt = $this->pdo->prepare("SELECT 'user_id' FROM 'users' WHERE 'email' = :email AND 'password' = :password");

$stmt = $this->pdo->prepare("SELECT user_id FROM users WHERE email = :email AND password = :password");

【讨论】:

以上是关于警告:PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parame的主要内容,如果未能解决你的问题,请参考以下文章

PDOStatement :: execute():SQLSTATE [HY093],批量插入时的奇怪行为

Mysql 错误 PDOStatement::execute(): MySQL server has gone away using Laravel and curl request

使用 PDO 登录后显示个人资料页面 [关闭]

SQL 错误 PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: 绑定变量的数量与中的标记数量不匹配

php之PDOStatement::execute数组参数带有键值会出错

在mysql中使用pdo的参数号无效问题