1
I get a json from an android application. This json is a java class that has been converted with Gson from java to a string. After the php web server receives this json I convert it to an array.
I want to do a generic Insert on my web server, where it automatically traverses the array and inserts it into the table passed by parameter.
ex:
public function create($array){
/*
* O nome da tabela é recebida por um post e é passada por um construtor
* $condição: Aqui eu teria que pegar os nomes das keys e deixar assim por exempo: nome, idade, sexo
* $valores: tenho que pegar o conteudo dos arrays. Ai tenho que verificar se é uma string ou data ou float para deixar entre aspas.Ex: 'rafael', 18, '1987-12-12'
*/
$query = "INSERT INTO $this->tabela ( $condicao ) VALUES ( $valores )";
$flag = mysqli_query($this->con, $query);
if($flag === TRUE){
$json['flag'] = TRUE;
} else {
$json['flag'] = FALSE;
}
mysqli_close($this->con);
return $json;
}
I don’t know if there’s an easier way to do what I want, or if there’s some ready-made api.
All help is welcome!
Why do you convert JSON data to Array? Why not insert the json data directly into the database? It’s much simpler.
– Asura Khan
Because I have relationships in the database.
– Rafael Silva
What do you mean by relationships? You can explain and give an example?
– Asura Khan
for example: a patient has a type and is also related to payment. How would I make this relationship between them?
– Rafael Silva
these relationships you create in the database itself. Through Inner Join ( left Join, full Outer Join) . This way when you apply a select, you can get related results. I could answer it better, but that would take away from the subject of your question.
– Asura Khan
but I would have to have the concept of keys in the bank... if I have a Patient table with a json field where I save the patient json, and another Payment table also with a json field where I save the payment json. in case I need to list all payments that a certain patient has made, how will I make this filter?
– Rafael Silva
And I think that it does not escape from the theme, because you are proposing a new solution to my problem. I think so.
– Rafael Silva
So I can send a reply saying that it would be better if you use the JSON object itself? I recommend you open a new question regarding the database relationships, because for sure I will receive a flag if I answer.
– Asura Khan
I think this is going to open up a security breach for you. Look at this: Mass assignment Vulnerability and this: SQL Injection.
– Victor Stafusa
Possible duplicate of How to save array to db?
– Daniel Omine