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']);
}
?>
Do so to display a possible error,
$credenciamento = mysql_query($queryinsert) or die(mysql_error());
– rray
Remember that all varchar or date fields need to be in single quotes.
– rray
Even if I put only the fields with int still won’t go
– Jonathan Willian
Error appears with code modification?
– rray
@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.
– Bacco
Excuse me, force of habit
– Jonathan Willian