我寻找示例来解决我的问题,但我很困惑。 如果我有这样的json数组:
$json = '{"a":"test","b":"test2"},{"a":"test3","b":"test4"}';
//decode to get as php variable
$obj = json_decode($json);
我的问题是当我尝试在数据库中插入值
时mysql_query("INSERT INTO suspiciousactivity (ID,Notes)
VALUES ('".$obj->{'a'}."','".$obj->{'b'}."')")or die(mysql_error());
我收到此错误:duplicate entry for key PRIMARY
如何从JsonArray
向我的数据库插入多个值?
答案 0 :(得分:1)
尝试使用:
$json = '{"a":"test","b":"test2"},{"a":"test3","b":"test4"}';
//decode to get as php variable
$arr = json_decode($json,true); //true to decode as a array not an object
mysql_query("INSERT INTO suspiciousactivity (ID,Notes)
VALUES ('".$arr[0]['a']."','".$arr[0]['b']."')")or die(mysql_error());
//use it as an array.