I'm looking for a php code to read the JSON object (which I'm going to upload using Android into the url) and I also want the php function to insert the data into my db. Can someone please help me? This is my JSON object
{
"id": "mID",
"description": "des",
"stars": "mStars",
"name": "avatar",
"year": "mYear",
"rating": "mRating",
"director": "mDirector"
"url": "www.dummy.com"
}
this is my php code
$app->post(
'/post/',
function () use ($app){
echo 'This is a POST route';
$json = $app->request->getBody();
$data = json_decode($json, true);
echo $data['name'];
echo $data['id'];
echo $data['description'];
echo $data['stars'];
echo $data['rating'];
echo $data['director'];
echo $data['url'];
echo $data['year'];
createMovie($data);
}
);
The following code is in a separate file. I have similar files with select statements which are working perfectly fine.
<?php
function createMovie($data) {
$conn=getDB();
if($stmt=$conn->prepare("$sql="INSERT INTO Movies (id, name,description, director, year, rating, stars, url)
VALUES ($data['id'],$data['name'],$data['description'],$data['director'],$data['year'],$data['rating'],$data['stars'],$data['url'])";
{
$stmt->execute();
$conn->close();
}
}
?>
when I type www.example.com/post/ I'm getting Error 404 not found