I have an array in php that contains some data, eg:
Array
(
[nome] => Nome
[sobrenome] => Sobrenome
[genero] => 1
[email] => [email protected]
)
And I would like to insert this data into a mySql table more automatically. When I generate this array, the process is already done so that it conforms to the table, that is, my table is made up of columns nome
, sobrenome
, genero
and email
. >
So, instead of doing a query like this:
INSER INTO tabela (nome, sobrenome, genero, email) VALUES ('$nome', ...)
I would like to know if there is any method of automating this execution based on the array of data I have.
I thought of executing some process to treat this array as follows:
foreach ($data as $key => $value){
$keys = $keys.$key.",";
$values = $values.$values.",";
}
$keys = substr($keys, 0,-1);
$values = substr($values, 0,-1);
INSER INTO tabela ('$keys') VALUES ('$values');
But I do not know if it is the most suitable / ideal and / or if there is a "correct" method of performing this task.