<code><?php $arr = [ "title" => "测试一下'单引'号保存问题", "name" => "tom" ]; $str = json_encode($arr); print_r($str); //将$str保存到数据库中,发现单引号失败; $sql = "UPDATE test SET str='" . $str . "' WHERE id=1"; $db->query($sql); ?> </code>
How is it usually solved? Directly replace single quotes?
<code><?php $arr = [ "title" => "测试一下'单引'号保存问题", "name" => "tom" ]; $str = json_encode($arr); print_r($str); //将$str保存到数据库中,发现单引号失败; $sql = "UPDATE test SET str='" . $str . "' WHERE id=1"; $db->query($sql); ?> </code>
How is it usually solved? Directly replace single quotes?
mysqli_real_escape_string()
PDO::quote
mysql_real_escape_string
Try not to use string splicing, use PDO and MYSQLi with Prepared Statement mechanism instead
Escape and save