I have a JSON object, which I have converted into a JSON string using the JSON.stringify() method in JavaScript. I then insert this into the database using AJAX posting to a PHP file.
$("#saveToDatabase").click(function(){
var place = searchBox.getPlaces();
var locationJson = JSON.stringify(place[0]);
$.ajax({
type: "POST",
url: "insertLocation.php",
dataType:"json",
ContentType:"application/json",
data: {
locationJson : locationJson
},
cache: false,
success: function(result){
window.alert("successful upload!");
}});
});
}
<?php
require_once("connection.php");
if(isset($_POST["locationJson"])){
$locationJson = $_POST['locationJson'];
$query ="INSERT INTO Locations (json) VALUES ('$locationJson')";
$statement = $pdo ->prepare($query);
$statement->execute();
}
?>
The problem I am having is that at somepoint as the data is being uploaded, the backslash "\" is being removed from my JSON strings. So when I select them from the database and try to manipulate them again in JavaScript, they are no longer valid JSON objects.
I am using SQL to upload to PHPmyAdmin.
Does anyone know a way around this? I need to store my JSON strings on the database without them being invalidated.
Cheers!