How do I mount pair of array key vaue to insert into table?

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 resolve this error:

    
asked by anonymous 10.09.2016 / 01:02

1 answer

0

Just simplify, do not need that array_fill() just call array_combine() with $keys and $vals . If you use prepared statements, do not include single quotes in values.

Switch:

$arr  = array_combine($keys,array_fill(0,count($keys),$vals));

By:

$arr  = array_combine($keys, $vals);

Your array will look like this:

Array
(
    [id] => '10'
    [name] => 'fulano'
)
    
10.09.2016 / 01:30