I need to insert elements of a php encoded array into a database and I'm completely stuck. I have first used json encode to to grab data from the database using an SQL query (which I did successfully) but I now need to be able to do the opposite. If anyone could help I'd greatly appreciate it. It's my second day of work and I'm not doing so well. The following is my code:
$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());