如何用php将多个json页面导入mysql数据库?

I use a php script to import a json feed into my database. The json feed has multiple pages. Like: www.website.com/file.json/?page=1, www.website.com/file.json/?page=2, etc. On every json page are stored 10 reviews. The script saves all pages into a local folder, named "json-pages". After saving the files, the scripts load the json pages(1.json, 2.json)into the database. Everything Works fine until now.

Unfortunately the script is not updating/adding database rows when a .json file is changed. It only works on the first run of the php script.

For example i have the file 1.json with 10 reviews and the file 2.json with 2 reviews.

Today we receive two new reviews. Then i got in total 4 reviews on the file 2.json. The script will not update the database tables from 12 to 14(new total) rows.

What am i doing wrong? I hope someone knows the trick.

JSON import php

database

This is my script:

// API KEY
DEFINE('API_KEY','xxx'); // SET API KEY         

// Setup database connection
include_once $_SERVER['DOCUMENT_ROOT'].'/wp-load.php';
$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD , DB_NAME);
mysqli_set_charset($con,"utf8");

// Check connection
if ($con->connect_error)
   die("Connection failed: " . $con->connect_error);    

global $wpdb;
$table_name = $wpdb->prefix . "custorate_beoordelingen";

// sql to create table
$sql = "CREATE TABLE $table_name (
  `CustorateID` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `title` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `date_created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `beoordeling` int(11) NOT NULL,
  `aanbevolen` int(11) NOT NULL,
  `tekst` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `klant_naam` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `reactie` int(11) NOT NULL
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";

//print table status
if(mysqli_multi_query($con, $sql)) {
    echo '<p style="text-align:center;"">Table created successfully</p>';
}       

// created a folder with json-pages         
DEFINE('DATA_FOLDER','./json-pages');

if (!is_dir(DATA_FOLDER)) {
    mkdir(DATA_FOLDER, 0755, true);
}

// setup json file                          
$file = file_get_contents("https://www.custorate.nl/json/".API_KEY."/");
$data = json_decode($file, true);           


// loop trough the number of pages 
for($i=1; $i <= $data['meta']['total_pages']; $i++){

    // use local data if exist, except the last one.
    if(file_exists(DATA_FOLDER.'/'.$i.'.json') && $i < $data['meta']['total_pages']){
        $file = file_get_contents(DATA_FOLDER.'/'.$i.'.json');
        $query = '';

    } else {
        // download the file if not exist. 
        $file = file_get_contents("https://www.custorate.nl/json/".API_KEY."/?start=".$i."");
        $query = '';
        if($file){
            $fp = fopen(DATA_FOLDER.'/'.$i.'.json',"w+");
            fputs($fp, $file);
            fclose($fp);
        }
    }

    // change received data to an Array
    $data = json_decode($file, true);

    if(count($data['beoordelingen'])){
        echo "<pre>";
        echo "<p>-- SQL Query --</p>";
        foreach($data['beoordelingen'] as $id=>$beoordeling){

            $insertPairs = array();
            foreach ($beoordeling as $key=>$val) {
                $insertPairs[addslashes($key)] = addslashes($val);
            }                       
            $insertKeys = '`' . implode('`,`', array_keys($insertPairs)) . '`';
            $insertVals = '"' . implode('","', array_values($insertPairs)) . '"';                   


            $query .= "INSERT INTO {$table_name}({$insertKeys}) VALUES ({$insertVals}); ";


            echo "INSERT INTO `{$table_name}` ({$insertKeys}) VALUES ({$insertVals});" . "
";

        } // end Foreach

        echo "</pre>";


        if(mysqli_multi_query($con, $query)) //Run Mutliple Insert Query
        {
             echo '<h3 style="text-align:center;"">JSON Data geïmporteerd</h3><br/>';
        } //endif                                   

    } // endif


}// end loop