简体   繁体   中英

Error trying to insert with PDO: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

I am getting this error: Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in

Array ( [0] => HY093 [1] => [2] => )

$arr = array();
    $values = "";
    $sql = "INSERT INTO avisos(nombre ";
    $values.=":nombre";
    $arr[] = "nombre => $nombre";

    if($plataforma!=""){
        $sql .= ", idPlataforma";
        $values.=", :idPlataforma";
        $arr[] = "idPlataforma => $plataforma";
    }
    if(idCompania!=""){
        $sql .= ", idCompania";
        $values.=", :idCompania";
        $arr[] = "idCompania => $compania";
    }
    if($fondo!=""){
        $sql .= ", idFondo";
        $values.=", :idFondo";
        $arr[] = "idFondo => $fondo";
    }
    if($remitente != ''){
        $sql .= ", idRemitente";
        $values.=", :idRemitente";
        $arr[] = "idRemitente => $remitente";
    }
    $sql.= ") VALUES ($values);";

    $stmt = $con->prepare($sql); 

    if ($stmt->execute($arr)){
        echo "OK";
    }

I can´t see where I´m failing.

The array you send to execute() needs to have key-value pairs where the keys are the placeholders and the values are the values you want to insert.

So instead of:

$arr[] = "nombre => $nombre";

You need:

$arr[':nombre'] = $nombre;

or:

$arr['nombre'] = $nombre;

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM