Insert data from an array into a mysql database

Asked

Viewed 1,547 times

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!

  • 1

    Why do you convert JSON data to Array? Why not insert the json data directly into the database? It’s much simpler.

  • Because I have relationships in the database.

  • What do you mean by relationships? You can explain and give an example?

  • for example: a patient has a type and is also related to payment. How would I make this relationship between them?

  • 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.

  • 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?

  • And I think that it does not escape from the theme, because you are proposing a new solution to my problem. I think so.

  • 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.

  • I think this is going to open up a security breach for you. Look at this: Mass assignment Vulnerability and this: SQL Injection.

  • 2

    Possible duplicate of How to save array to db?

Show 5 more comments

2 answers

1

You can use a code like this, it’s a template for you to have a basis

$campos      = implode(', ', array_keys($array));
$valores     = '"'.implode('", "', $array).'"'; 
$query       = "INSERT INTO $this->tabela 
                              ( $campos ) 
                       VALUES ( $valores )"; 

0

A suggestion to solve your problem. You said you need to recover the values of a JSON and that for this you want to transform them into an array.

There is a function in PHP, which retrieves data from a JSON and transforms it into objects.

Follow an example

$obj = json_decode($json_recebido);

echo "nome: " . $obj->$nome . " idade: " $obj->$idade . " sexo: " . $obj->sexo;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.