Sebsemillia
Sebsemillia

Reputation: 9476

PDO Error on insert

I know that the source of my problem must be somewhere in my statement, but I just can't find it. Maybe you have a better eye than me right now.

I get the following error while trying to insert a row in my table:

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

Here is my insert function:

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();
    }

And here a sample object I'm passing:

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"
}

Upvotes: 1

Views: 36

Answers (2)

lpg
lpg

Reputation: 4937

You have 8 vars in sql query:

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

And 9 vars in your array/object.

Upvotes: 2

Dave
Dave

Reputation: 888

you're sending in 9 parameters ( the extra is ID ) and that's not allowed, check out: http://php.net/manual/en/pdostatement.execute.php

specifically:

input_parameters .... You cannot bind more values than specified; if more keys exist in input_parameters than in the SQL specified in the PDO::prepare(), then the statement will fail and an error is emitted.

Upvotes: 1

Related Questions