7
I am doing a function using PDO, when I try to use the PREPARE method the function does not end successfully, replace PREPARE with QUERY changing some arguments and worked.
But my question is this because with PREPARE is not working?
function create($tabela, array $dados){
$campos = implode(", ", array_keys($dados));
$values = "'".implode("', '", array_values($dados))."'";
$pdo = new PDO('mysql:host=localhost;dbname=curso','root','');
try{
$operacao = $pdo->prepare("INSERT INTO $tabela (?) VALUES (?)");
$operacao->bindValue(1,$campos);
$operacao->bindValue(1,$values);
$operacao->execute();
}catch(PDOException $e){
echo 'Erro '.$e->getMessage();
}
if($operacao->rowCount()>0){
return true;
}else{
echo "Não Cadastrou";
}
};
$evento = array('id_cliente' => 81, 'nome_cliente' => 'Marcos', 'idade' => 32);
create('clientes',$evento);
You probably won’t have a field called "name,address,phone" in your table, but are trying to bind it. The syntax of
bindValue
isbindValue ( mixed $parameter , mixed $value [, int $data_type = PDO::PARAM_STR ] )
, you can’t pass a collection of fields like this.– Bacco
You cannot bind column names only in values.
– rray
Hello Friends, but even if I try. Only Bind in the values is not right,
– Michael Alves