Php 从json数据中转义单引号解决方案无效

Php 从json数据中转义单引号解决方案无效,php,json,Php,Json,我正在从一个网站获取json数据。。但在数据中,只有一个引用导致了问题。你会建议我做什么我已经试过了stru_replace不起作用请帮忙 这是代码 $string=file_get_contents('http://website'); $string=str_replace("'", "\'", $string);//not working $json=json_decode($string); foreach($json as $p) { $query="INSERT INTO abc(i

我正在从一个网站获取json数据。。但在数据中,只有一个引用导致了问题。你会建议我做什么我已经试过了stru_replace不起作用请帮忙

这是代码

$string=file_get_contents('http://website');
$string=str_replace("'", "\'", $string);//not working
$json=json_decode($string);
foreach($json as $p)
{
$query="INSERT INTO abc(id,
name,
address,
address2,
storeID,
) VALUES('$p->id',
'$p->name',
'$p->address',
'$p->address2',
'$p->storeID',
)";
mysql_query($query) or die(mysql_error());
}
如果使用str_替换,则会出现此错误


警告:为foreach()提供的参数无效。
您正在用单引号替换单引号。相反,您希望:

$string = str_replace("'", '"', $string);
此外,不能将用户输入插入SQL语句。使用或用户输入

$string = str_replace("'", '"', $string);

这种方式

您有SQL注入漏洞。请考虑使用PDO编写准备语句。