我寻找示例来解决我的问题,但我很困惑。 如果我有一个像这样的 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
插入多个值到我的数据库?
最佳答案
尝试使用:
$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.
关于php - 如何将 JSON 数组插入 Mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14736880/