0

我寻找例子来解决我的问题,但我很困惑。如果我有一个这样的 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到我的数据库中?

4

1 回答 1

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.
于 2013-02-06T19:17:18.957 回答