How do I perform a php Insert where data is passed on by a json that has an object

Asked

Viewed 32 times

1

How do I perform an Insert in php where the data is passed by a json that has in the object a nonprimitive variable.

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 a 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 save the PK id of type and user in the database.

  • json is not valid missing a "{" in "user"

2 answers

0

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

Change:

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

To:

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

0

Besides, we had to use

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

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

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

Browser other questions tagged

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