我有這樣的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