2016-11-14 323 views
-4

我有這樣的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可以在數據庫中保存

+0

刪除'$ tempservice'你完全不需要它。循環你的數據並執行插入。 – brzuchal

+1

如果您是此代碼的OC,請問您爲什麼使用準備好的聲明並仍然注入數據? –

+0

你的腳本存在[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

回答

-5
foreach($data['response']['list'] as $value) { 
    $sql = "insert into master_service(code,name,rate,withvalue) Values ('{$value['code']}','{$value['name']}', '{$value['rate']}', '{$value['withValue']}')"; 
    $st = $dbh->prepare($sql); 
    $st->execute ($data); 
} 

對不起,我忘了上json_decode真實參數..編輯

+1

TYPO,你可能想修改'Values('$ value) - > code'' – RiggsFolly

+0

而且OP已經使用了'json_decode($ response,true);'這個值是一個數組而不是對象 – RiggsFolly

+1

這段代碼永遠不會工作。另一個可接受的答案是欺詐 –

-2

你可以試試這個: -

include_once 'db_connect.php'; 
$data = json_decode($response, true); 

$tempservice = []; 
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){ 
     $withValue = ($service['withValue']) ? 1 : 0; 
     $tempservice[$service['code']] = ['name'=>$service['name'], 'rate'=>$service['rate'], 'withvalue'=>$withValue]; 
    } 
} 

foreach($tempservice as $key =>$value) { 
    $sql = "insert into master_service(code, name, rate, withvalue) Values ('$key', '".$value['name']."', '".$value['rate']."', '".$value['withvalue']."')"; 
    $st = $dbh->prepare($sql); 
    $st->execute(); 
} 

注:請修復任何語法錯誤。你也可以跳過創建tempservice陣列,可以執行INSERT SQL有

+0

此代碼將永遠不會工作 –

+0

@YourCommonSense請指出問題的位置,以便我可以輕鬆地解釋或查看我的觀點。謝謝 –

+0

大聲笑。人們喜歡投票,因爲它就像一個點擊而不是解釋。在我的代碼中,我試圖指出要改變的地方,並在筆記中告訴我要改正。我知道問題在哪裏。但是,沒有人有時間來創建環境,執行代碼並將其粘貼到這裏,這只是一種常識。 –

1

只需使用一個foreach循環,並添加缺少的列,無需使用重組$tempservice陣列:

include_once 'db_connect.php'; 
$data = json_decode($response, true); 

// Simplified if, checking for isset($data['response') is redundant 
if (isset($data['response']['list'])) 
{ 
    // Prepare the statement using placeholders 
    $sql = "INSERT INTO master_service (code,name,rate,withValue) VALUES (:code,:name,:rate,:withValue)"; 
    $stmt = $dbh->prepare($sql); 

    foreach($data['response']['list'] as $row) 
    { 
     // Execute query with parameters 
     $stmt->execute($row); 
    } 
} 

重要:我更換了使用佔位符查詢變量。這樣可以防止SQL Injection的安全。我還將prepare放在循環之外,以便它在每次迭代時不會「重複」。

+1

提示:您可以將$ row直接發送到執行,確實避免了內部循環 –

+0

@YourCommonSense。使它變得更簡單(可能更少出錯)。我相應地更新了我的答案。 – simon