I'm saving Backbone model data, which POSTs a JSON object to my save.php file. As the model data will be for my application's users, I'd like to store unique values in a MySQL table.
Currently, I'm using this method:
$values = json_decode(file_get_contents('php://input'), true);
$name = $values["name"];
$sql="INSERT INTO `users` (name) VALUES $name";
It's giving me this error
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'John Doe' at line 1
If I pass a simple string in the query, it works the way I'd like:
$sql="INSERT INTO `users` (name) VALUES ('John Doe')";
My questions are:
- Why is $name not a string?
- Is this the best way to insert a JSON object into a MySQL table?
Thank you!
$nameis the problem anyway.