How do I perform an insert in php where the data is passed by a json that has an object

1

How do I perform an insert in php where the data is passed by a json that has a non-primitive variable in the object.

This is my Json

{"id":0,"latitude":-8.0326941,"longitude":-34.9287402,"status":1, 
"tipovaga":{"descricao":"no aplication","id":5},         
 "usuario":"email":"[email protected]","id":14,"nome":"nome","palavrasecreta":"secreto","password":"XXXX"}
}

This is webservice

$stmt = $conn->prepare("INSERT INTO flanelinha_db.HistoricoVaga (tipo_vaga,usuario, latitude, longitude, status, endereco, bairro, cidade,   estado) VALUES (?,?,?,?,? , null,null,null,null)");

$json = json_decode(include("novavaga.json"));  
$tipovaga  = $json ->{'tipovaga'};
$usuario   = $json->{'usuario'};    
$latitude    = $json->{'latitude'};
$longitude   = $json->{'longitude'};
$status     = $json->{'status'};

$stmt->bind_param("iiddi", $tipovaga, $usuario, $latitude, $longitude ,$status);
$stmt->execute();
$stmt->close();
$id = $conn->insert_id;

I need to write the id of the type of PK and user type in the database.

    
asked by anonymous 23.09.2016 / 08:52

2 answers

0

To get json in this case do not use include the right is file_get_contents ()

Change:

$json = json_decode(include("novavaga.json"));

To:

$json = json_decode(file_get_contents("novavaga.json"));
    
23.09.2016 / 14:17
0

In addition,

// the second paramenter converts to array $ arr = json_decode ($ json, 1);

and then // retrieving the id value of the tipovaga     $ tipovaga = ($ arr ['tipovaga'] ['id']);

In this way I solved the problem thanks to the help of a colleague in the facebook group.

    
25.09.2016 / 20:48