错误:插入值列表与列列表不匹配:1136列数与第1行的值计数不匹配

时间:2015-08-10 21:07:17

标签: php mysql

尝试将值插入MySQL表后,我收到此错误。

  

插入值列表与列列表不匹配:1136列数与第1行的值计数不匹配

TABLE:

CREATE TABLE `users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password` char(64) COLLATE utf8_unicode_ci NOT NULL,
  `first_name` char(255) COLLATE utf8_unicode_ci NOT NULL,
  `last_name` char(255) COLLATE utf8_unicode_ci NOT NULL,
  `salt` char(16) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

PHP:

$query = "
        INSERT INTO 
            users (username, password, first_name, last_name, salt, email) 
            VALUES (:username, :password, :first_name, :last_name :salt, :email) ";

    $query_params = array( 
        ':username' => $_POST['username'], 
        ':password' => $password, 
        ':first_name' => $_POST['first_name'],
        ':last_name' => $_POST['last_name'],
        ':salt' => $salt, 
        ':email' => $_POST['email'] 
    );

    try { 
        // Execute the query to insert user into table
        $statement = $database->prepare($query); 
        $result = $statement->execute($query_params); 
    } catch(PDOException $ex) {
        $response["success"] = 0;
        $response["message"] = "Insert failed".$ex->getMessage();
        die(json_encode($response));
    }

我不理解这个问题,并希望得到任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:4)

缺少逗号:

[..snip..]:first_name, :last_name :salt, :email) ";
                                 ^---

由于您遗漏了,,因此您的6个字段INSERT只有5个值可用。