0
$key = "id,name";
$val = "'$lastid','$autor'";
...
if ($email){
$key .= ",email";
$val .= ",'$email'";
}
...
$keys = \explode(',', $key);
$vals = \explode(',', $val);
$arr = array_combine($keys,array_fill(0,count($keys),$vals));
....
//O array montado fica assim:
// INSERT INTO autores (id, name, email) VALUES (:id, :name, :email)
$insok = $pdo->insert($arr);
if (!$insok) {
$error = "Erro ao inserir dados<br>".print_r($conn->errorInfo());
}
echo $error; // mostra o seguinte resultado
//array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL }
I would like to know how to solve this mistake:
Hello, thank you so much for the quick answer. The problem really was in the single quotes. The correct is: $key = "id, name"; $val = $id. " ," . $cauthor; E also simplified the array_combine() as you said. Worked ok. A hug.
– user1818765
@user1818765 with
array_fill()
gets like this:Array
(
 [id] => Array
 (
 [0] => '10'
 [1] => 'fulano'
 )

 [name] => Array
 (
 [0] => '10'
 [1] => 'fulano'
 )

)
– rray