【问题标题】:Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: [duplicate]带有消息“SQLSTATE [HY093]”的未捕获异常“PDOException”:参数号无效:[重复]
【发布时间】:2017-01-13 05:41:52
【问题描述】:
<?php
    include_once 'session.php';
    include 'database.php';
    class user{

        private $db;

        public function __construct(){
                $this->db= new Database();


        }   
        public function userRegistration($data){
                    $name         = $data['name'];
                    $email          = $data['email'];
                    $password    = md5($data['password']);
                    $chk_email = $this->emailCheck($email);





                                            if($name == "" or  $email == "" or $password == "" ){
                                                $msg = "<div class = ' alert alert-danger'><strong>Error !</strong>Field must not be empty</div>" ;   //bootstrap aleart massage 
                                                return $msg;
                                            }


                                                         if(filter_var($email,FILTER_VALIDATE_EMAIL)=== false){  // email validate
                                                                $msg = "<div class ='alert alert-danger'><strong>Error ! </strong>Email address is not valid</div>";
                                                                return $msg;

                                                        }

                                            if ($chk_email==true){

                                            $msg = "<div class ='alert alert-danger'><strong>Error ! </strong>This email is already exist</div>";
                                            return $msg;

                                    }

                            $sql = "INSERT INTO register_tbl(name,username,email,password,gender) VALUES(:name,:username,:email,:password,:gender)";
                            $query = $this->db->pdo->prepare($sql);
                            $query->bindValue(':name',$name);
                            $query->bindValue(':email',$email);
                            $query->bindValue(':password',$password);           
                            $result=$query->execute();//problem is here ..
                            if($result){
                                $msg = "<div class = 'alert alert-success'><strong>Success .</strong>You have register now.</div>";
                                return $msg;

                            }else{
                                $msg = "<div class = 'alert alert-danger'><strong>Sorry !</strong>Some thing is not right</div>";
                            }


        }



                public function emailCheck($email){
                    $sql = "SELECT email FROM register_tbl WHERE email = :email ";
                    $query = $this->db->pdo->prepare($sql ); // prepare() is the method of PDO class;
                    $query->bindValue(':email',$email);  //bindValue() is the method of PDO class;
                    $query->execute();
                    if($query->rowCount()>0){
                        return true;
                    }else{
                        return false;
                    }       
                }

                public function getLoginUser($email,$password){
                    $sql = "SELECT * FROM register_tbl WHERE email = :email AND password = :password LIMIT 1";
                    $query = $this->db->pdo->prepare($sql ); // prepare() is the method of PDO class;
                    $query->bindValue(':email',$email);  //bindValue() is the method of PDO class;
                    $query->bindValue(':password',$password); 
                    $query->execute();
                    $result = $query->fetch(PDO::FETCH_OBJ);
                    return $result;

                }

        public function userLogin($data){

            $email          = $data['email'];
            $password    = md5($data['password']);

                                            if( $email == "" or $password == "" ){
                                                $msg = "<div class = ' alert alert-danger'><strong>Error !</strong>Field must not be empty</div>" ;   //bootstrap aleart massage 
                                                return $msg;
                                            }
                                             if(filter_var($email,FILTER_VALIDATE_EMAIL)=== false){  // email validate
                                                $msg = "<div class ='alert alert-danger'><strong>Error ! </strong>Email address is not valid</div>";
                                                return $msg;                                        

                                            if ($chk_email==true){

                                            $msg = "<div class ='alert alert-danger'><strong>Error ! </strong>This email is not exist</div>";
                                            return $msg;

                                    }
                                $result = $this->getLoginUser($email,$password);
                                if($result){
                                     Session :: init();
                                     Session :: setinit('login',true);
                                     Session :: setinit('id',$this->id);
                                     Session :: setinit('name',$this->name);
                                     Session :: setinit('username',$this->username);
                                     Session :: setinit('loginmsg',"<div class ='alert alert-success'><strong>Success ! </strong>You are login.</div>");

                                    header('index.php');

                                }else{echo "<script class = 'alert alert-danger'><strong>Error</strong>some thing wrong</script>";}


        }

}
    }

?>

致命错误:未捕获的异常“PDOException”和消息“SQLSTATE[HY093]:无效的参数编号:绑定变量的数量与 /opt/lampp/htdocs/project/log/user.php 中的标记数量不匹配”: 51 堆栈跟踪:#0 /opt/lampp/htdocs/project/log/user.php(51):PDOStatement->execute() #1 /opt/lampp/htdocs/project/log/registration.php(9): user->userRegistration(Array) #2 {main} 在第 51 行的 /opt/lampp/htdocs/project/log/user.php 中抛出 请帮帮我....

【问题讨论】:

  • 参数计数错误。就这么简单。
  • $query-&gt;bindValue(':gender',$gender); + 用户名相同。这就是错误消息告诉您的内容。 (编辑:两者都还没有定义)
  • 3 不等于 5
  • 并且不要使用 md5

标签: php


【解决方案1】:

您在 SQL 上指定了 5 个参数:

:name,:username,:email,:password,:gender

但是你只绑定了3个:

$query->bindValue(':name',$name);
$query->bindValue(':email',$email);
$query->bindValue(':password',$password);

【讨论】:

  • “您在 SQL 上指定了 4 个参数” - 1...2...3...4...5
  • @Fred-ii- 您评论了 2 次,但您只需要一次。
  • @AbraCadaver 这家伙不知道 4 和 5 之间的区别 lol 还是我数错了? stackoverflow.com/revisions/41623367/1
  • @Fred-ii- 抱歉,打错了...
  • 谢谢。非常感谢……..
猜你喜欢
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 2017-04-26
相关资源
最近更新 更多