简体   繁体   中英

PHP Mysql Update Serialize Array in escape apostrophe

I have a problem for php mysql update serialized data.

My Serialized data

a:1:{i:0;a:3:{s:5:"image";s:4:"5812";s:5:"title";s:14:"Day 1 : LOREM";s:4:"desc";s:416:"Lorem Ipsum is 'simply dummy' text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500's, when an unknown printer ";}}

Problem is in the text like, 'simply dummy' for apostrophe.

Mysql Update statement

$conSave="update serialized_data set value='$str1' where id='{$_POST['key_id']}'";
$conSaveData = mysql_query($conSave);

How can I solve this problem in serialized data?

MySQLi Update Statement

$stmt =  $con->stmt_init();
$stmt->prepare("Update serialized_data set value=? WHERE id = ?");
$stmt->bind_param("ss",$a,$b);
$a = $str1;
$b = $_POST['key_id'];

$stmt->execute();

You code should look like this

try {
   $b = $_POST['key_id'];

   $stmt = new mysqli("example.com", "user", "password", "database");
   $stmt->prepare("Update serialized_data set value=? WHERE id = ?");
   $stmt->bind_param("si",$str1, $b);
   $stmt->execute();

} catch(Exception $e){
   echo $e->getMessage();
}

However, I would rather use json string instead of serialized string.

Something like this

$data = array('key' => 'val');  // this is your original array before you serialize it
$a = json_encode($data);  //this will convert your array to a json string

then when you select the string from the database, you will use json_decode($selectedString); // this will convert your json string into an object json_decode($selectedString); // this will convert your json string into an object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM