I have a wordpress based site with an html form that saves data in mysql database through json. I did it in a way like this:
<?php
global $wpdb;
$db = $wpdb->prefix . 'db_table';
$json_query = $wpdb->get_row(
"
SELECT app, ts
FROM $db
WHERE user_id = $user_id
",
ARRAY_A
);
$json = json_decode($json_query[app], true);
?>
<label>Family Name</label>
<input type="text" name="json[family_name]" <?php echo ($json_ok[family_name] ? 'value="' . $json_ok[family_name] . '"' : ''); ?> />
<?php
$json_send = json_encode( $_POST['json'] );
if (isset($_POST['save'])) {
$wpdb->update(
$db,
array(
'app' => $json_send
),
array( 'user_id' => $user_id )
);
?>
It works but if user insert the apostrophe character the sistem save apostrophe preceded by a backslash. Every time user saves, the sistem add a backslash. Now I have the database full of backslashes like this:
{"family_name":"Rossi","indirizzo":"strada d\\\\\\\\\\\\\\\\\\\'oro","first_name":"Maurizio","country_birth":"Italy","city_birth":"Roma"}
How can i solve this? Thanks
I have dealt with the same issue myself when saving and using json encoded stings in wordpress. This is what solved it for me.
Just make a call to stripslashes() when displaying value in input;
like so:
<input type="text" name="json[family_name]" <?php echo (stripslashes($json_ok[family_name]) ? 'value="' . stripslashes($json_ok[family_name]) . '"' : ''); ?> />
When it gets saved again it will stop adding those extra slashes. As mentioned in the comment by John Bell when you make a call to the update function it is calling addslashes().