I’m having an issue with inserting JSON into a database, my intention is to take the variables, json_encode them, remove slashes (from magic_quotes), and then addslashes back in to escape the quotes in {“key”:”value”}
Unfortunately, strip_slashes on the encoded string isn’t doing anything, and outputs this
{"content":"<p>This string has it\'s downsides</p>","date":1271352514}
I have then tried addslashes, and mysql_real_escape_string, both output
"{\\"content\\":\\"<p>This string has it\\\'s downsides</p>\\",\\"date\\":1271352514}"
I can’t work out why it’s adding in two slashes? And I’m tearing my hair out over this, everytime I try to stripslashes it leaves one in, and adding slashes adds two. Any help would be hugely appreciated!
First, you should really consider turning
magic_quotesoff… To quote the manual:That being said, use
json_encode()to build your JSON array (instead of building your own), and finish off with a single call tomysql_real_escape_string()while querying as such:Also, the PHP group recommends you use
mysqliinstead ofmysql. Its Object Oriented API and support for parametrized queries greatly improve both development speed, code maintenance and security.Here is the above code written using
mysqli: