我有来自 UNITY3d WWWForm 的表单数据到我的 php 文件。我用 json 文件类型创建了一个列,现在我想将该字符串存储在数据库中。我还希望 id 字段成为数组其余部分的数组的键。
include('dbconfig.php');
$id= $_POST['ID'];
$servicedate=$_POST['ServiceDate'];
$servicelocation=$_POST['ServiceLocation'];
$mileage=$_POST['Mileage'];
$labor=$_POST['Labor'];
$oilbrand=$_POST['OilBrand'];
$oilprice=$_POST['OilPrice'];
$filterbrand=$_POST['FilterBrand'];
$filterprice=$_POST['FilterPrice'];
$oilfilterpurchaselocation=$_POST['PurchasePlace'];
$arr = array('Service Date' => $servicedate,
'Service Location' => $servicelocation,
'Mileage' => $mileage,
'Labor' => $labor,
'Oil Brand' => $oilbrand,
'Oil Price' => $oilprice,
'Filter Brand' => $filterbrand ,
'Filter Price' => $filterprice ,
'Purchase Place' =>$oilfilterpurchaselocation);
$json= json_encode($id => $arr); //Is this part right?
echo 'Here is our new JSON';
echo $json;
$var= $db->prepare("INSERT into json VALUES(?,?,?,?,?)";
foreach($data as $row)
{
$var->bindParam(1, $row['id']);
$var->bindParam(2, $row['']);
$var->bindParam(3, $row['']);
$var->execute();
}
这是我需要的更多吗?谢谢!
我昨天看了代码,忘记在插入的值部分添加引号。当我运行它时,我得到了一个不同的错误,所以我知道我正在取得进展!事实证明,有人告诉我这是否正确,我不得不将所有列更改为具有默认值。当我将它们全部更改为 NULL 时,我能够插入 json 数据。如果其他人在此问题上苦苦挣扎,这里是您需要的插入命令。
$sql= "INSERT INTO column_name (name of parameter) VALUES ('$json')";
$result = mysqli_query($ms, $sql);
if(!$result)
{
echo' Item Not Inserted! '. mysqli_error($ms);
}
else
{
echo"Item Inserted!";
}
确保你做了一个 json_encode 来制作一个可以放在数据库中的 json 对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句