2017-06-15 62 views
1

代碼有問題嗎? 這不是將數據插入數據庫,我不知道什麼是錯的。 有人可以幫我嗎?從json文件添加到mysql db數據

這裏是PHP代碼

<?php 
 

 
include 'config.php'; 
 

 
$data = file_get_contents('programs.json'); 
 
$json = json_decode($data, TRUE); 
 
if ($data != '' && count($json) > 0) { 
 
    $sql = "SELECT pid FROM programs"; 
 
    $result = mysqli_query($conn, $sql); 
 
    $transArr = array(); 
 
    if (mysqli_num_rows($result) > 0) { 
 
     while ($row = mysqli_fetch_assoc($result)) { 
 
      $transArr[] = $row["pid"]; 
 
     } 
 
    } 
 
    $bulk_query = ''; 
 
    $bulk_update_query = ''; 
 
    $pid = ''; 
 
    $pname = ''; 
 
    $start_date = ''; 
 
    $end_date = ''; 
 
    $logo = ''; 
 
    $short_description = ''; 
 
    $description = ''; 
 
    $ended_date = ''; 
 
    $url = ''; 
 
    $program_status = ''; 
 
    $summary = ''; 
 
    $categories = ''; 
 
    foreach ($json as $key => $jsonRow) { 
 
     
 
     
 
     $pid = isset($jsonRow['id']) ? $jsonRow['id'] : ''; 
 
     $pname = isset($jsonRow['name']) ? $jsonRow['name'] : ''; 
 
     $start_date = isset($jsonRow['start_date']) ? $jsonRow['start_date'] : ''; 
 
     $end_date = isset($jsonRow['end_date']) ? $jsonRow['end_date'] : ''; 
 
     $logo = isset($jsonRow['logo']) ? $jsonRow['logo'] : ''; 
 
     $short_description = isset($jsonRow['short_description']) ? $jsonRow['short_description'] : ''; 
 
     $description = isset($jsonRow['description']) ? $jsonRow['description'] : ''; 
 
     $ended_date = isset($jsonRow['ended_date']) ? $jsonRow['ended_date'] : ''; 
 
     $url = isset($jsonRow['url']) ? $jsonRow['url'] : ''; 
 
     $program_status = isset($jsonRow['program_status']) ? $jsonRow['program_status'] : ''; 
 
     $summary = isset($jsonRow['summary']) ? floatval($jsonRow['summary']) : ''; 
 
     $categories = isset($jsonRow['parent_category']['name']) ? $jsonRow['parent_category']['name'] : ''; 
 
     
 

 
     if (!in_array($pid, $transArr)) { 
 
      $bulk_query.="('$pid','$pname','$start_date','$end_date','$logo','$short_description','$description',$ended_date,$url,'$program_status','$summary','$categories'),"; 
 
     } else { 
 
      $sql = "UPDATE programs SET pid='$pid',name='$pname',start_date='$start_date',end_date='$end_date'," 
 
        . "logo='$logo',short_description='$short_description',description='$description',ended_date=$ended_date,url=$url," 
 
        . "program_status='$program_status',summary='$summary',categories='$categories' WHERE pid=$pid"; 
 
      mysqli_query($conn, $sql); 
 
     } 
 
    } 
 
    $bulk_query = rtrim($bulk_query, ","); 
 
    $sql = "INSERT INTO programs (pid,pname,start_date,end_date,logo,short_description,description,ended_date,url,program_status,summary,categories) VALUES $bulk_query"; 
 
    $conn->query($sql); 
 
    createFileBackup(); 
 
    echo ' Record Added'; 
 
} 
 

 
?>

JSON數據我嘗試插入到數據庫

[ 
 
    { 
 
     "id": 12356, 
 
     "name": "test", 
 
     "start_date": "2017-06-14T21:00:00+00:00", 
 
     "end_date": null, 
 
     "logo": "test.png", 
 
     "short_description": "test short_description", 
 
     "description": "test description", 
 
     "ended_date": null, 
 
     "url": "http://www.test.com/", 
 
     "program_status": "Active", 
 
     "commissions": { 
 
      "details": [ 
 
       { 
 
        "category": { 
 
         "id": 1, 
 
         "name": "Sales", 
 
         "cookie_length": 30, 
 
         "type": "sale", 
 
         "payout_type": "percent", 
 
         "tiers": [ 
 
          { 
 
           "tier": { 
 
            "level": 1, 
 
            "action": "12.00", 
 
            "subaction": "12.00", 
 
            "formatted": { 
 
             "action": "12,00%", 
 
             "subaction": "12,00%" 
 
            } 
 
           } 
 
          } 
 
         ] 
 
        } 
 
       } 
 
      ], 
 
      "summary": { 
 
       "percent": "12,00%", 
 
       "flat": "-" 
 
      }, 
 
      "tiers": { 
 
       "count_type": "clicks", 
 
       "amount": 1, 
 
       "levels": [] 
 
      } 
 
     }, 
 
     "categories": [ 
 
      { 
 
       "parent_category": { 
 
        "name": "test", 
 
        "child_categories": [ 
 
         { 
 
          "child_category": { 
 
           "name": "test2" 
 
          } 
 
         } 
 
        ] 
 
       } 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "id": 123456, 
 
     "name": "test", 
 
     "start_date": "2017-06-14T21:00:00+00:00", 
 
     "end_date": null, 
 
     "logo": "test.png", 
 
     "short_description": "test short_description", 
 
     "description": "test description", 
 
     "ended_date": null, 
 
     "url": "http://www.test.com/", 
 
     "program_status": "Active", 
 
     "commissions": { 
 
      "details": [ 
 
       { 
 
        "category": { 
 
         "id": 1, 
 
         "name": "Sales", 
 
         "cookie_length": 30, 
 
         "type": "sale", 
 
         "payout_type": "percent", 
 
         "tiers": [ 
 
          { 
 
           "tier": { 
 
            "level": 1, 
 
            "action": "12.00", 
 
            "subaction": "12.00", 
 
            "formatted": { 
 
             "action": "12,00%", 
 
             "subaction": "12,00%" 
 
            } 
 
           } 
 
          } 
 
         ] 
 
        } 
 
       } 
 
      ], 
 
      "summary": { 
 
       "percent": "12,00%", 
 
       "flat": "-" 
 
      }, 
 
      "tiers": { 
 
       "count_type": "clicks", 
 
       "amount": 1, 
 
       "levels": [] 
 
      } 
 
     }, 
 
     "categories": [ 
 
      { 
 
       "parent_category": { 
 
        "name": "test", 
 
        "child_categories": [ 
 
         { 
 
          "child_category": { 
 
           "name": "test2" 
 
          } 
 
         } 
 
        ] 
 
       } 
 
      } 
 
     ] 
 
    } 
 
    ]

和DB結構

enter image description here

+0

更精確。你有任何錯誤或什麼?實施一些錯誤處理,這可以幫助你很多。 – Twinfriends

+0

在我的託管錯誤日誌中沒有錯誤,你能提供給我錯誤處理的代碼添加嗎? – user3899237

+0

你有什麼消息嗎?或者頁面只是空白? – Twinfriends

回答

1

我解決這個問題這個$ NEW_NAME = str_replace函數( 「'」, 「 ''」, 「$ PNAME」);我在網上找到,現在我可以插入到帶有撇號的數據庫變量。感謝您的幫助