My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How to create PHP script to parse elements from the app?

Atul kumar's photo
Atul kumar
·Oct 25, 2017

I have this kind of data sent from app which i want to store in the database this is the data sent from the app:-

OrderSummary=[{"Cost":"500","Name":"Wine","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"Wine","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"Wine","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"whisky","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"whisky","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"whisky","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"whisky","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"whisky","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"whisky","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"whisky","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"whisky","Quantity":"1","Sellerid":"2","Price":"500"},{"Cost":"500","Name":"whisky","Quantity":"1","Sellerid":"2","Price":"500"}]

now iam able to store the data in an text file but iam unable to create an script to store it in the database This is the php script which i have created but iam unable to proceed further to do the database operation and store it in the database :-

<?php
$i=0;
$password="";
$user="root";
$database="shadowpets";
$host="localhost";
$response=array();
$con=mysqli_connect($host,$user,$password,$database)or die("Unable to connect");
if($_SERVER["REQUEST_METHOD"]=="POST")
{


    if(isset($_POST['Order']))
    {

        $data=$_POST['Order'];
        $file='text.txt';
        $json=json_decode($data,true);
         $result=file_put_contents($file,$data);  


            $response["success"]=1;
            $response["message"]="done";

        $response["success"]=1;
        $response["message"]="done";
     }
    else
    {
        $response["success"]=0;
        $response["message"]="parameters not correctl formatted";
    }

    echo json_encode($response);
}
    ?>

can anybody tell me how i can change it and make it work?