我需要将 php 编码数组的元素插入数据库,但我完全卡住了。我首先使用 json 编码来使用 SQL 查询(我成功地完成了)从数据库中获取数据,但我现在需要能够做相反的事情。如果有人可以提供帮助,我将不胜感激。这是我工作的第二天,我做得不太好。以下是我的代码:
$UserCoords_Query = "Select Accuracy, Longitude, Latitude, Timestamp
FROM UserDetails
WHERE UserId =" . $UserID;
$UserCoords_result = mysql_query($UserCoords_Query);
if (mysql_num_rows($UserCoords_result) == 0) {
echo "There are no users with an id of ". $UserID;
}
else {
$EmptyArray=array();
while ($row = mysql_fetch_array($UserCoords_result)) {
$Accuracy=$row['Accuracy'];
$Longitude= $row['Longitude'];
$Latitude=$row['Latitude'];
$Timestamp= $row['Timestamp'];
$Queue= array('Accuracy:' => $Accuracy, 'Latitude' => $Latitude, 'Longitude' => $Longitude, 'Timestamp' => $Timestamp);
array_unshift($EmptyArray,$Queue);
}
$ObjectResponse = array('Coords' => $EmptyArray);
echo json_encode($ObjectResponse);
$Json_Encoded= json_encode($ObjectResponse);
$Json_Decoded= json_decode($Json_Encoded, true);
$Update_Query= "INSERT INTO UserDetails (UserId, Accuracy, Latitude, Longitude, Timestamp)
VALUES ('".$UserID."','".$Json_Decoded[0] ['Accuracy']."','".$Json_Decoded[0]['Latitude']."',
'".$Json_Decoded[0]['Longitude']."','".$Json_Decoded[0]['Timestamp']."')";
mysql_query($Update_Query) or die(mysql_error());