dqajyxqem115006813 2016-09-18 10:29
浏览 74
已采纳

异常'PDOException',消息'SQLSTATE [HY093]:参数号无效

This look a lot simple but for some reasons i can not get it to work... I need all the help i can get

 function register_user($email, $password,  $gender, $phone_number){
    $this->sql = "insert into user
                  (
                  email_addr, phone_number, password,
                  gender, activation_code, isMobileVerified,
                  last_login_date, unix_sign_up_time, sign_up_date
                  ) VALUES
                  (
                  :email, :phone_number, :password,
                  :gender, :activation_code, :isMoobileVerified,
                  :last_login, :time_unix, NOW()
                  )";
    $this->prepare($this->sql);
    $this->bind(':email', $email);
    $this->bind(':phone_number', $phone_number);
    $this->bind(':password', $password);
    $this->bind(':gender', $gender);
    $this->bind(':activation_code', sha1($password));
    $this->bind(':isMobileVerified', 0);
    $this->bind(':last_login', time());
    $this->bind(':time_unix', time());

    $this->execute();

    return $this->lastInsertedId();
}

when i run this function like this

try{
    echo "<br>" . $i->register_user('myMail@register.com', 'password', 'male', '2348020000007');
}catch (PDOException $e) {
    //todo: logging function or mail to dev goes here
    echo $e ."<br>". $e->getMessage();
}

i get this error

exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in C:\wamp\www\ecommerce\system\model\class.ecommerce.php:215 Stack trace: #0 C:\wamp\www\ecommerce\system\model\class.ecommerce.php(215): PDOStatement->bindValue(':isMobileVerifi...', true, 5) #1 C:\wamp\www\ecommerce\system\model\class.ecommerce.php(281): ukorJidechi\db_handler->bind(':isMobileVerifi...', true) #2 C:\wamp\www\ecommerce amespace_test.php(51): ukorJidechi\ecommerce_user->register_user('myMail@register...', 'password', 'male', '2348020000007') #3 {main} SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

here is my custom bind method

function bind($placeholder, $value, $type = null){
    if (is_null($type)) {
        switch (true) {
            case is_int($value):
                $type = \PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = \PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = \PDO::PARAM_NULL;
                break;
            default:
                $type = \PDO::PARAM_STR;
        }
    }
    $this->stmt->bindValue($placeholder, $value, $type);
}

Can someone show me what I am doing wrong? Please.

  • 写回答

1条回答 默认 最新

  • dougan1205 2016-09-18 10:32
    关注

    See this statement here,

    $this->bind(':unix_sign_up_time', time());
    

    And compare the above statement with $this->sql

    $this->sql = "insert into user
                  (
                  ...
                  :last_login, :time_unix, NOW()
                                ^^^^^^^^^
                  )";
    

    So your bind() statement should be,

    $this->bind(':time_unix', time());
    

    Edited:

    See this line here in $this->sql,

    $this->sql = "insert into user
                  (
                  ...
                  :gender, :activation_code, :isMoobileVerified,
                                              ^^^^^^^^^^^^^^^^^^
                  ...
                  )";
    

    It should be :isMobileVerified

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探