这个用于SQL插入的PHP PDO语句有什么问题?

时间:2013-04-28 15:41:32

标签: php sql pdo

我已经存储了用户名,密码和其他变量,但我不断收到错误“无效参数编号:绑定变量数与该标记数不匹配”:

$database->query(
    'INSERT INTO users_inactive(verCode, username, password, email, date, type) 
    VALUES (:vercode, :username, :password, :email, :date, :type)', 
    array(
        ':vercode' => $verCode, 
        ':username' => $username, 
        ':password' => $password, 
        ':email' => $email, 
        ':date' => $date, 
        ':type'=>'customer')
);

它有什么问题吗?我确保在user_inactive表格中可以使用这些列中的每一列。

这是$ database包装函数:

    public function query($query, $bind = null, $fetch = 'FETCH_ASSOC') {
    /* Prepare the query statement */
    $this->statement = $this->pdo->prepare($query);
    /* Bind each value supplied from $bind */
    if($bind != null) {
        foreach($bind as $select => $value) {
            /* For each type of value give the appropriate param */
            if(is_int($value)) {
                $param = PDO::PARAM_INT; 
            } elseif(is_bool($value)) {
                $param = PDO::PARAM_BOOL;
            } elseif(is_null($value)) {
                $param = PDO::PARAM_NULL;
            } elseif(is_string($value)) {
                $param = PDO::PARAM_STR;
            } else {
                $param = FALSE;
            }
            /* Bid value */
            if($param) {
                $this->statement->bindValue($select, $value, $param);
            }
        }
    }
    /* Execute Query & check for any errors */
    if(!$this->statement->execute()){
        $result = array(
            1 => 'false',
            2 => '<b>[DATABASE] Error - Query:</b> There was an error in sql syntax',
        );
        return $result;
    }
    /* Return all content */
    if($fetch == 'FETCH_ASSOC') {
        $result = $this->statement->fetch(PDO::FETCH_ASSOC);
    } elseif($fetch == 'FETCH_BOTH') {
        $result = $this->statement->fetch(PDO::FETCH_BOTH);
    } elseif($fetch == 'FETCH_LAZY') {
        $result = $this->statement->fetch(PDO::FETCH_LAZY);
    } elseif($fetch == 'FETCH_OBJ') {
        $result = $this->statement->fetch(PDO::FETCH_OBJ);
    } elseif($fetch == 'fetchAll') {
        $result = $this->statement->fetchAll();
    }
    return $result;
}
  }

取自Tutis登录。

3 个答案:

答案 0 :(得分:0)

只是一些线索,你可能会遗漏一些冒号。看看这里

PHP Mysql PDO number of bound variables does not match number of tokens

或者可能在你的冒号上有一些区分大小写

答案 1 :(得分:0)

错误似乎出现在你的包装函数中。 PDO::PARAM_NULL == 0,因此此值不会通过

if($param) {
    ...
}

因此,您不要为NULL值调用bindValue()。相反,使用:

if ($param !== FALSE) {
    ...
}

编辑 - 再次阅读您的代码,这将是foreach()块的更好代码,在将来产生更少的神秘错误消息:)

    foreach($bind as $select => $value) {
        /* For each type of value give the appropriate param */
        if(is_int($value)) {
            $param = PDO::PARAM_INT; 
        } elseif(is_bool($value)) {
            $param = PDO::PARAM_BOOL;
        } elseif(is_null($value)) {
            $param = PDO::PARAM_NULL;
        } elseif(is_string($value)) {
            $param = PDO::PARAM_STR;
        } else {
            // Report error about invalid type and return from the function
            ...
        }
        // we should bind every value from $bind array, unconditionally!
        $this->statement->bindValue($select, $value, $param);
    }

答案 2 :(得分:-1)

试一试:

<?php
$sql = $database->prepare(
'INSERT INTO users_inactive(verCode, username, password, email, date, type) 
VALUES (:vercode, :username, :password, :email, :date, :type)');
$sql->execute(array(
    'vercode' => $verCode, 
    'username' => $username, 
    'password' => $password, 
    'email' => $email, 
    'date' => $date, 
    'type'=>'customer')
);