Php read and insert JSON into Mysql database

0

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

Json that 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"}]}

Code php:

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]
                                                }
                                            });
    
asked by anonymous 20.04.2018 / 22:04

0 answers