我有这样的JSON数据:JSON阵列到阵列PHP多维
{
"response": {
"count": 212,
"list": [
{
"code": "02007",
"name": "swept the room",
"rate": 750000,
"withValue": false
},
{
"code": "02005",
"name": "mop room",
"rate": 600000,
"withValue": false
},
{
"code": "02003",
"name": "buying food",
"rate": 175000,
"withValue": false
}
]
},
"metaData": {
"message": "OK",
"code": 200
}
}
,我有表模式是这样的:
mysql> desc master_service; +----------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------------+-------------+------+-----+---------+----------------+ | id | int(25) | NO | PRI | NULL | auto_increment | | code | varchar(10) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | rate | double | YES | | NULL | | | withvalue | tinyint(1) | YES | | NULL | | +----------------+-------------+------+-----+---------+----------------+
和我的编码是这样。
//使用PHP PDO
include_once 'db_connect.php';
$data = json_decode($response, true);
$tempservice = array();
if(isset($data['response']) && isset($data['response']['list']))
{
//use foreach on ['response']['list'] index - here are teachers data stored
foreach($data['response']['list'] as $service)
$tempservice[$kesadaran['code']] = $service['name'];
}
foreach($tempservice as $key =>$value) {
$sql = "insert into master_service(code,name) Values ('$key','$value')";
$st = $dbh->prepare($sql);
$st->execute ($data);
}
它只能保存在代码和名称形式的数据库。我想率和withValue可以在数据库中保存
删除'$ tempservice'你完全不需要它。循环你的数据并执行插入。 – brzuchal
如果您是此代码的OC,请问您为什么使用准备好的声明并仍然注入数据? –
你的脚本存在[SQL注入攻击]的风险(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) 看看发生了什么[小鲍比表](http://bobby-tables.com/)即使 [如果你是逃避投入,它不安全!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets -around-mysql-real-escape-string) 使用[prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly