Insert data into Mysql database received from a Json

Asked

Viewed 1,567 times

1

I have the following problem, I have an app and I send some data in Json format, to the server, until ai blz, enough good in this format: {"credenciador":"","evento":"16","inscrito":[{"id_inscrito":"13","data_credenciado":"2016-09-23 14:39:52"}],"atividade":"8"}

I have to receive this data and add in the database (Mysql), the problem is that it will not at all, if I take out the 'INSERT' the code works well, but with the 'INSERT' of the error, I believe that my sql is giving a null result for some reason, Has anyone ever had that problem or any idea what it might be? Below is the script where I handle the Json values:

<?php
    include('../admin/config.php');
    function processaCredenciamento($credenciamento){
        $credenciamento = json_decode($credenciamento);
        $credenciador = $credenciamento->credenciador;
        $evento = $credenciamento->evento;
        $inscritos = $credenciamento->inscrito;

        $inscritos = json_decode($inscritos);

        $queryinsert = "insert into credenciamento(credenciador, inscrito, evento, data_credenciamento, data_envio) values ";

        foreach ($inscritos as $inscrito) {
            $queryinsert = "(".$credenciador.",".$inscrito->id_inscrito.",".$evento.",".$inscrito->data_credenciado.",now()),";
        }
        $queryinsert = substr($queryinsert, 0, -1);

        $credenciamento = mysql_query($queryinsert);


    }

    if($_POST){
         echo '{"retorno":true}';
         processaCredenciamento($_POST['credenciamento']);
         $filename = "retorno_credenciamento.txt";
         file_put_contents($filename, $_POST['credenciamento']);
    }

FUNCTIONAL CODE:

<?php
    include('config.php');

if($_POST){
        $credenciamento = json_decode($_POST['credenciamento']);
        $credenciador = $credenciamento->credenciador;
        $evento = $credenciamento->evento;
        $atividade = $credenciamento->atividade;
        $inscritos = $credenciamento->inscrito;

        foreach ($inscritos as $in){
            $insert_cred = "INSERT INTO credenciamento (evento, atividade, inscrito, data_credenciameno, data_envio) VALUES (".$evento.", ".$atividade.", ".$in->id_inscrito.", '".$in->data_credenciado."', now())";
            $credenciamento = mysql_query($insert_cred, $conexao) or die (mysql_error());
        }

         echo '{"retorno":true}';
         $filename = "retorno_credenciamento.txt";
         file_put_contents($filename, $_POST['credenciamento']);
}

?>
  • 1

    Do so to display a possible error, $credenciamento = mysql_query($queryinsert) or die(mysql_error());

  • 1

    Remember that all varchar or date fields need to be in single quotes.

  • Even if I put only the fields with int still won’t go

  • Error appears with code modification?

  • 1

    @Jonathanwillian removed again the [solved] of the title. To close the subject, just the Accept (the blue V, which you put in the answer given), does not need (and should not) touch the initial post in these cases.

  • Excuse me, force of habit

Show 1 more comment

1 answer

1


When using Decode, use true after JSON to transform into an array, I believe it is more practical.

$inscrito = json_decode($inscritos,true);

So when accessing the content use $inscrito['id_inscrito'], if you don’t use true after decoding, access the content using $inscrito->{'id_inscrito'}.

If even with the modifications does not work, after mounting your query, before executing, print it and try to run directly in the database to see the message that gives, this way you can keep trying to change the query until you find where the error is, this way you are sure it is an error in mysql and not in PHP.

You can do for example: a die($queryinsert);

Edit: Change the variable declaration

    $credenciamento = json_decode($credenciamento,true);
    $credenciador = $credenciamento['credenciador'];
    $evento = $credenciamento['evento'];
    $inscritos = $credenciamento['inscrito'];

  // $inscritos = json_decode($inscritos); creio que está linha não será mais necessária
  • Now it worked, friend, but still not being able to take the values of $inscrito, from this error: json_decode() expects Parameter 1 to be string, array Given in. Even putting the 'true'

  • Oh yes, it is that in the first Code you already transform into object, I will edit the answer

  • It worked out thank you very much! I will put as it was the code in the update

Browser other questions tagged

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