-1
I have an array in php that contains some data, example:
Array
(
[nome] => Nome
[sobrenome] => Sobrenome
[genero] => 1
[email] => [email protected]
)
And I would like to insert this data into a mysql table more automatically. At the moment that Gero this array, the process is already done so that it is in accordance with the table, that is, my table is composed by columns nome
, sobrenome
, genero
and email
.
So instead of doing a query like this:
INSER INTO tabela (nome, sobrenome, genero, email) VALUES ('$nome', ...)
I wonder if there is any method to automate this execution based on the data array I have.
I thought I’d run 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 appropriate/ideal and/or if there is a "correct" method to perform this task.
suggestion
– rray
@rray seems to with
array_keys
andarray_values
I can simplify the code I used as a suggestion, correct?– celsomtrindade
Yes is a way to solve the problem just don’t forget that the array keys should have the same column names.
– rray
@rray this I’m already doing just to try to automate to the maximum this process.
– celsomtrindade
@Danielomine blz, thank you!
– Guilherme Nascimento