尝试使用PDO插入时出错:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:参数未定义

时间:2017-11-30 10:19:01

标签: php mysql pdo

我收到此错误: 警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:参数未在

中定义

数组([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";
    }

我无法看到我失败的地方。

1 个答案:

答案 0 :(得分:3)

您发送到execute()的数组需要具有键值对,其中键是占位符,值是您要插入的值。

所以而不是:

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

你需要:

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

或:

$arr['nombre'] = $nombre;