插入时PDO错误

时间:2014-09-16 21:51:30

标签: php pdo prepared-statement

我知道我的问题的根源必须在我的陈述中的某个地方,但我找不到它。也许你现在的眼睛比我好一些。

尝试在表格中插入一行时出现以下错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

这是我的插入功能:

private function _insert()
    {
        $sql = 'INSERT INTO shows (
            thetvdb_id,
            name,
            imdb_id,
            language,
            overview,
            genre,
            lastupdated,
            status
            )' .
            ' VALUES (
            :thetvdb_id,
            :name,
            :imdb_id,
            :language,
            :overview,
            :genre,
            :lastupdated,
            :status
            )';
            $abfrage = self::$db->prepare($sql);
            $abfrage->execute($this->toArray(false));
            //Setze die ID auf den von der DB generierten Wert
            $this->id = self::$db->lastInsertId();
    }

这是我传递的示例对象:

object(Show)#201 (9) {
  ["id":"Show":private]=>
  int(0)
  ["thetvdb_id":"Show":private]=>
  string(5) "74875"
  ["name":"Show":private]=>
  string(10) "The Closer"
  ["imdb_id":"Show":private]=>
  string(9) "tt0458253"
  ["language":"Show":private]=>
  string(2) "en"
  ["overview":"Show":private]=>
  string(358) "Deputy Police Chief Brenda Leigh Johnson (Kyra Sedgwick) is a police detective who transfers from Atlanta to Los Angeles to head up a special unit of the LAPD that handles sensitive, high-profile murder cases. Despite a tendency to step on people\'s toes, Johnson manages to convert even her strongest adversaries with her unique ability to get to the truth."
  ["genre":"Show":private]=>
  string(21) "|Crime|Drama|Mystery|"
  ["lastupdated":"Show":private]=>
  string(10) "1410004625"
  ["status":"Show":private]=>
  string(5) "Ended"
}

2 个答案:

答案 0 :(得分:2)

您在sql查询中有8个变量:

        :thetvdb_id,
        :name,
        :imdb_id,
        :language,
        :overview,
        :genre,
        :lastupdated,
        :status

数组/对象中有9个变量。

答案 1 :(得分:1)

您正在发送9个参数(额外是ID)并且不允许这样做,请查看: http://php.net/manual/en/pdostatement.execute.php

具体是:

  

input_parameters .... 您不能绑定多于指定值的值; if   input_parameters中存在的键多于   PDO :: prepare(),然后语句将失败并发出错误。