Php read and insert JSON into Mysql database

Asked

Viewed 54 times

0

I am trying to perform an Insert of this json with php , but it is not being sent to the mysql database.

Json I’m trying to read:

{"questionario":[{"id_base":"609","id_usuario":"1","id_pesquisa":"1","id_varejo":"16","respostas":"[{"Pergunta":"Nome","Resposta":"asdasd"},{"Pergunta":"Você é responsável pelo recebimento da entrega de parceria ?","Resposta":"não"}]","data_inicio":"2018-04-20 16:56:58"}]}

Php code:

public function saveResult($post) {

    $array = $post;

    foreach ($array['questionario'] as $value) {
        $ar['id_usuario']  = $value['id_usuario'];
        $ar['id_pesquisa'] = $value['id_pesquisa'];
        $ar['id_varejo']   = $value['id_varejo'];
        $ar['id_base']     = $value['id_base'];
        $ar['data_inicio'] = $value['data_inicio'];
        foreach($value['respostas'] as $value){
            $ar['Pergunta'] = $value['Pergunta'];
            $ar['Resposta'] = $value['Resposta'];
            try{
            $db = Database::conexao();
            $qry = "INSERT INTO `resultado`(`id_pesquisa`,`id_varejo`,`id_usuario`,`id_base`,`pergunta`,`resposta`,`data_inicio`)VALUES(
              :idPesquisa,
              :idVarejo,
              :iduser,
              :idbase,
              :pergunta,
              :resposta,
              :data_inicio)";

            $statement = $db->prepare($qry);
            $statement->bindValue(':idPesquisa', $value['id_pesquisa']);
            $statement->bindValue(':idVarejo', $value['id_varejo']);
            $statement->bindValue(':iduser', $value['id_usuario']);
            $statement->bindValue(':idbase', $value['id_base']);
            $statement->bindValue(':pergunta', $value['Pergunta']);
            $statement->bindValue(':resposta', $value['Resposta']);
            $statement->bindValue(':data_inicio', $value['data_inicio']);
            $statement->execute();    

         } catch (PDOException $e){
                 echo $e->getMessage();
                 $db->rollBack(); 
         }
        }
    }
}

Generates the JSON responses :

var modifiedData = Object.keys(result.data).map(function(qName) {
                                                return {
                                                  Pergunta: qName,
                                                  Resposta: result.data[qName]
                                                }
                                            });
  • Does it make a mistake? I would be careful with the names of the variables mainly the $value.

  • the strange thing is that it does not generate any error to debug

  • Utilize $db->errorInfo to check for errors.

  • Also no error returns, I believe it is in foreach... but I’m not able to return the data in them

  • 1

    This json of the question returns invalid: https://ideone.com/XTLSBs

  • True rray, this incorrect, but even in this format would it be possible to read all the data in php? I added the code that generates this part of JSON

Show 1 more comment
No answers

Browser other questions tagged

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