I have no idea about PHP can anyone help me how can I store nested JSON data into the database? I write code but it is for a single row at a time but If I want to parse nested JSON how can I?
[
{
"product_name": "Product3",
"product_price": "500",
"product_quantity": "5",
"userid": "1",
"orderid": "1"
},
{
"product_name": "Product4",
"product_price": "500",
"product_quantity": "5",
"userid": "1",
"orderid": "1"
},
{
"product_name": "Product5",
"product_price": "500",
"product_quantity": "5",
"userid": "1",
"orderid": "1"
}
]
// Creating MySQL connection.
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
// Storing the received JSON in $json.
$json = file_get_contents('php://input');
// Decode the received JSON and store into $obj
$obj = json_decode($json,true);
$productid = $obj['product_id'];
$productname = $obj['product_name'];
$productprice = $obj['product_price'];
$productquantity = $obj['product_quantity'];
$userid = $obj['userid'];
$orderid = $obj['orderid'];
$query = "INSERT INTO order_product (product_id, product_name, product_price, product_quantity, userid, orderid) VALUES ('$productid','$productname','$productprice','$productquantity','$userid','$orderid')";
if(mysqli_query($con,$query)){
// On query success it will print below message.
$MSG = 'Data Successfully Submitted.' ;
// Converting the message into JSON format.
$json = json_encode($MSG);
// Echo the message.
echo $json ;
}
else{
echo 'Try Again';
}
I am trying to insert using Postman but only a single blank row inserted. Help me please how can store into the database after hit API?