Error inserting into mysql DB with PHP PDO

1

I'm doing an insert with PHP PDO to a mysql database, I pass an array for the parameters, but it works and I get the following error SQLSTATE [HY093]: Invalid parameter number: parameter was not defined

this is the connection method

public function conectar(){
    $conexion_mysql = "mysql:host=".$this->host.";dbname=".$this->base."";
    $conexionBD= new PDO($conexion_mysql,$this->usuario,$this->password);
    $conexionBD->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    //esta linea arregla la codificacion de caracteres utf8
    $conexionBD->exec("set names utf8");

    return $conexionBD;
}

here is the method where the transaction is executed

public function add(){
    $sql="INSERT INTO  PERSONAS 
                              (PA_PAIS_ID, 
                              PE_NOMBRE,
                              PE_CORREO,
                              PE_CLAVE,
                              PE_ESTADO)

                              VALUES(:PA_PAIS_ID,:PE_NOMBRE,:PE_CORREO,:PE_CLAVE,1)";
    $arreglo=array(":PE_PAIS_ID"=>1,":PE_NOMBRE"=>"Jose Miguel",":PE_CORREO"=>"[email protected]", ":PE_CLAVE"=>123);
    try {
          $db       = $this->conectar();
          $ejecutar = $db->prepare($sql);
          return $ejecutar->execute($arreglo);
    } catch (PDOException $e) {
        echo '{"error": {"text": '.$e->getMessage().'}';    


    }

}

also probe the query if it had syntax error but it does not have, which can be.

    
asked by jose miguel jara 10.09.2018 в 00:03
source

1 answer

2

You have an error in the following line;

VALUES(:PA_PAIS_ID,:PE_NOMBRE,:PE_CORREO,:PE_CLAVE,1)";

You have a 1 at the end and this is accepted as a value, since you should have it without the 1

You should have it like that;

 VALUES(:PA_PAIS_ID,:PE_NOMBRE,:PE_CORREO,:PE_CLAVE,:PE_ESTADO)";

Anyway you lack that value in the array, if you do not misunderstand you wanted to place the 1 as value, you can still pass it in the array anyway;

$arreglo=array(":PE_PAIS_ID"=>1,":PE_NOMBRE"=>"Jose Miguel",":PE_CORREO"=>"[email protected]", ":PE_CLAVE"=>123,":PE_ESTADO"=>"1");
    
answered by 10.09.2018 / 00:14
source