2017-06-15 1 views
1

Stimmt etwas nicht mit dem Code? Es ist nicht die Daten in die Datenbank einfügen, ich weiß nicht, was falsch ist. Kann mir bitte jemand helfen? Hierzu mysql hinzufügen db-data aus der json-datei

ist der PHP-Code

<?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'; 
 
} 
 

 
?>

Die JSON-Daten im db

[ 
 
    { 
 
     "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" 
 
          } 
 
         } 
 
        ] 
 
       } 
 
      } 
 
     ] 
 
    } 
 
    ]

Und die DB-Struktur ist

einzufügen versuchen,

enter image description here

+0

Genauer gesagt. Hast du irgendwelche Fehler oder etwas ähnliches? Implementieren Sie eine Fehlerbehandlung, die Ihnen sehr helfen könnte. – Twinfriends

+0

gibt es keine Fehler in meinem Hosting-Fehlerprotokoll, können Sie mir bitte mit Fehlerhandling Code zum Hinzufügen? – user3899237

+0

Erhalten Sie irgendwelche Nachrichten? Oder ist die Seite einfach leer? – Twinfriends

Antwort

1

Ich behebe das Problem mit diesem $ new_name = str_replace ("'", "' '", "$ pname"); das fand ich online und jetzt kann ich in db vars mit Apostrophen einfügen. Vielen Dank für Ihre Hilfe

Verwandte Themen