我有从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"}]
现在我可以将数据存储在文本文件中,但是我无法创建脚本来将其存储在数据库中 这是我创建的php脚本,但我无法继续进行数据库操作并将其存储在数据库中: -
<?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);
}
?>
任何人都可以告诉我如何改变它并让它发挥作用吗?
答案 0 :(得分:0)
您需要做的是将此$数据解码为json,然后访问每个值
$data
是一个Json数组,获取每个元素,然后访问它们类似于键值对。
$dataDecoded = json_decode($data,true);
foreach($dataDecoded as $singleArray)
{
/*Here I am printing,
You can use it to insert to DB, Insert into table values ..........
*/
echo $singleArray['Cost'];
echo "<br/>"; //Just to place new line
echo $singleArray['Name'];
echo "<br/><br/>"; //Just to place new line
/*
similarly echo others ..........
*/
}