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?
Working with objects in any coding language can get intimidating, especially when you have to navigate the nodes. Whenever I'm faced with a dataset I don't understand, I use json_encode's JSON_PRETTY_PRINT constant (or throw it in an online JSON parser) to look at the data's structure. I recommend trying that first:
echo json__encode($response, PRETTY_PRINT_);You'll notice your data is an array of objects. You can see that from the brackets([]) wrapping all your objects({}) together. So if we tried to access one of the form data, we'd have to reference the first entry in the array:
$json = json_encode($response); $single_item = $json[0]; $single_cost = $json[0]->Cost;Or we can loop through each array item to print out all of them:
foreach($json as $item) { print_r($item .'\n'); echo $item->Cost . '<br />'; echo $item->Quantity . '<br />'; }This isn't the only way to work with objects/arrays in PHP, so feel free to explore and experiment with what method works best for you! Some programmers prefer using SimpleXML to work with structured data like this.
For the SQL it'd look something like this:
$sql = "INSERT INTO yourtable (column1,column2) VALUES ('$data', '$variable')";Then you run the SQL using your
$conclass you created and it'squeryfunction. I put it in aif/elsestatement to check if it worked or not, and dump the results into the JS console so you don't muddle any code with echo's or prints://check for errors - print in JS console if ($con->query($sql) === TRUE) { ?> <script> console.log('Insert Success'); </script> <?php } else { ?> <script> console.log('Insert Error <?php echo $name ?>'); </script> <?php }