Search code examples
phpmysqlpdobindparam

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined'


First: Sorry, I know, this question has been asked a million times. And I know this is a human error that I'm probably missing a bindStatement somewhere down the line. Can you help me find it?


I'm having the following problem with PDO and prepared statements.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in /home/midelive/public_html/creapizza/model/temaHelper.php:64 Stack trace: #0

That is the error message. It happens for two methods on the same class, and its driving me nuts

code N1:

  public function create(tema $t, $idUser){
    $pdo_options[PDO::ATTR_ERRMODE]=PDO::ERRMODE_EXCEPTION;  ///username, Pass, DB
$conn= new PDO($this->connection,$this->admin,$this->pass,$pdo_options);
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $tipo=$t->tipo;
    $nombreEmpresa=$t->titulo;
    $logo=$t->logo;
    $subtitulo=$t->subtitulo;
    $simbolo1=$t->simbolo1;
    $simbolo2=$t->simbolo2;
    $simbolo3=$t->simbolo3;
    $texto1=$t->txt1;
    $texto2=$t->txt2;
    $texto3=$t->txt3;
    $linkSitio=$t->link;
    $linkFacebook=$t->linkfb;
    $descripcion=$t->descripcion;

    $stmt=$conn->prepare(sprintf("INSERT INTO tema  (idUsuario, tipoTema, nombreEmpresa, logo, 
        subtitulo,simbolo1, simbolo2, simbolo3, texto1, texto2, texto3, linkFacebook, Descripcion)
         VALUES (:idUsuario,:tipoTema,:nombreEmpresa,:logo,:subtitulo, :simbolo1, simbolo2, :simbolo3, 
         :texto1, :texto2, :texto3,:linkFacebook,:Descripcion)"));
    $stmt->bindParam(':idUsuario', $idUser);
    $stmt->bindParam(':tipoTema', $tipo);
   $stmt->bindParam(':nombreEmpresa',$nombreEmpresa);
    $stmt->bindParam(':logo',$logo);
    $stmt->bindParam(':subtitulo', $subtitulo);
    $stmt->bindParam(':simbolo1',$simbolo1);
    $stmt->bindParam(':simbolo2',$simbolo2);
    $stmt->bindParam(':simbolo3',$simbolo3);
    $stmt->bindParam(':texto1',$texto1);
    $stmt->bindParam(':texto2',$texto2);
    $stmt->bindParam(':texto3',$texto3);
    $stmt->bindParam(':linkFacebook',$linkFacebook);
    $stmt->bindParam(':Descripcion',$descripcion);

    $stmt->execute();
    $statement=$conn->query("SELECT LAST_INSERT_ID()");
$lastID=$statement->fetch(PDO::FETCH_NUM);
return $lastID[0];
}

Code N2:

public function create(tema $t, $idUser){
    $pdo_options[PDO::ATTR_ERRMODE]=PDO::ERRMODE_EXCEPTION;  ///username, Pass, DB
$conn= new PDO($this->connection,$this->admin,$this->pass,$pdo_options);
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $tipo=$t->tipo;
    $nombreEmpresa=$t->titulo;
    $logo=$t->logo;
    $subtitulo=$t->subtitulo;
    $simbolo1=$t->simbolo1;
    $simbolo2=$t->simbolo2;
    $simbolo3=$t->simbolo3;
    $texto1=$t->txt1;
    $texto2=$t->txt2;
    $texto3=$t->txt3;
    $linkSitio=$t->link;
    $linkFacebook=$t->linkfb;
    $descripcion=$t->descripcion;

    $stmt=$conn->prepare(sprintf("INSERT INTO tema  (idUsuario, tipoTema, nombreEmpresa, logo, subtitulo,simbolo1, simbolo2, simbolo3, 
            texto1, texto2, texto3, linkFacebook, Descripcion) VALUES (:idUsuario , :tipoTema , :nombreEmpresa , :logo , :subtitulo ,
            :simbolo1 , :simbolo2 , :simbolo3 , :texto1 , :texto2 , :texto3 , :linkFacebook , :Descripcion)"));
    $stmt->bindParam(':idUsuario', $idUser);
    $stmt->bindParam(':tipoTema', $tipo);
    $stmt->bindParam(':nombreEmpresa',$nombreEmpresa);
    $stmt->bindParam(':logo',$logo);
    $stmt->bindParam(':subtitulo', $subtitulo);
    $stmt->bindParam(':simbolo1',$simbolo1);
    $stmt->bindParam(':simbolo2',$simbolo2);
    $stmt->bindParam(':simbolo3',$simbolo3);
    $stmt->bindParam(':texto1',$texto1);
    $stmt->bindParam(':texto2',$texto2);
    $stmt->bindParam(':texto3',$texto3);
    $stmt->bindParam(':linkFacebook',$linkFacebook);
    $stmt->bindParam(':Descripcion',$descripcion);

    $stmt->execute();
    $statement=$conn->query("SELECT LAST_INSERT_ID()");
$lastID=$statement->fetch(PDO::FETCH_NUM);
return $lastID[0];
}

I'm going kinda crazy counting over and over the statements and the query. I did a ctrl+F search to check that I did not mispelled any of those, but found nothing.


Solution

  • If you cannot manage to make named parameters match, go for positional placeholders. At least to spot an error you will only have to count the question marks.