简体   繁体   中英

How to import multiple table records from json to mysql using php

I have the json file from multiple table multi records. The json file is created from local mysql database and I also have another online database with same schema. But every table does not have the same number of fields and same data/value. But some of the field names are same for all tables (id, added_on, last_updated). I want to import these json file values to the online database by executing a single php file.

The json file as like this:

[
    {
        "tableName":"table_Name_1",
        "rows":[
                {   
                    "t1column1":"valuet1row11",
                    "t1column2":"valuet1row12",
                    "t1columnx":"valuet1row1x"
                },
                {   
                    "t1column1":"valuet1row21",
                    "t1column2":"valuet1row21",
                    "t1columnx":"valuet1row2x"  
                },
                {   
                    "t1column1":"valuet1rowx1",
                    "t1column2":"valuet1rowx2",
                    "t1columnx":"valuet1rowxx"  
                }
               ]
    },
        {
        "tableName":"table_Name_2",
        "rows":[
                {   
                    "t2column1":"valuet2row11",
                    "t2column2":"valuet2row12",
                    "t2columnx":"valuet2row1x"
                },
                {   
                    "t2column1":"valuet2row21",
                    "t2column2":"valuet2row22",
                    "t2columnx":"valuet2row2x"  
                },
                {   
                    "t2column1":"valuet2rowx1",
                    "t2column2":"valuet2rowx2",
                    "t2columnx":"valuet2rowxx"
                }
               ]
    },
        {
        "tableName":"table_Name_n",
        "rows":[
                {   
                    "tncolumn1":"valuetnrow11",
                    "tncolumn2":"valuetnrow12",
                    "tncolumnx":"valuetnrow1x"
                },
                {   
                    "tncolumn1":"valuetnrow21",
                    "tncolumn2":"valuetnrow22",
                    "tncolumnx":"valuetnrow2x"  
                },{ 
                    "tncolumn1":"valuetnrowx1",
                    "tncolumn2":"valuetnrowx2",
                    "tncolumnx":"valuetnrowxx"
                }
               ]
    },
]

The following php code is used to import the json file records to a single table in the database. (Here the single_table.json only contains a single table records)

<?php
    try
    {
        $connect = mysqli_connect("localhost", "fmart", "password", "mart_dbsync");    
        $query = '';
        $table_data = '';
        $filename = "single_table.json";

        $data = file_get_contents($filename);
        $array = json_decode($data, true); 

        foreach($array as $row) 
        {
            $query .= "INSERT INTO purchases(id, invoicenum, supplier, stock_keeper, counter, added_by, is_deleted, description, is_opening_stock, department, added_on, last_updated) VALUES ('".$row["id"]."', '".$row["invoicenum"]."', '".$row["supplier"]."', '".$row["stock_keeper"]."', '".$row["counter"]."', '".$row["added_by"]."', '".$row["is_deleted"]."', '".$row["description"]."', '".$row["is_opening_stock"]."', '".$row["department"]."', '".$row["added_on"]."', '".$row["last_updated"]."') ON DUPLICATE KEY UPDATE invoicenum='".$row["invoicenum"]."', supplier='".$row["supplier"]."', stock_keeper='".$row["stock_keeper"]."', counter='".$row["counter"]."', added_by='".$row["added_by"]."', is_deleted='".$row["is_deleted"]."', description='".$row["description"]."', is_opening_stock='".$row["is_opening_stock"]."', department='".$row["department"]."', added_on='".$row["added_on"]."', last_updated='".$row["last_updated"]."';";
        }

            mysqli_multi_query($connect, $query);  

            echo "<h1>All purchases appended </h1>";
    } 

    catch(Exception $e)
    {   
        echo $e->getMessage();    
    }
?>

In the above php code the table name is hard coded in the INSERT statement. But by using the new json (as the json format above) it contains more than 25 tables and the table name should be taken from the json file.

here is the echo'd results:

contacts

Insert query:

INSERT INTO contacts 
(First_Name, Last_Name, Company, Business_Phone, Email_Address)
VALUES
('Dave','Frank','Company1','0115 999999','zvv@zz.com'),
('Dave','Blogs','Company2','0115 888888','zvv@zz.com'),
('David','frank','Company3','0115 777777','zvv@zz.com')

Error: 1
INSERT INTO contacts (First_Name, Last_Name, Company, Business_Phone, Email_Address) VALUES ('Dave','Frank','Company1','0115 999999','zvv@zz.com'), ('Dave','Blogs','Company2','0115 888888','zvv@zz.com'), ('David','frank','Company3','0115 777777','zvv@zz.com')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1contacts_old

Insert query:

INSERT INTO contacts_old 
(First_Name, Last_Name, Company, Business_Phone, Email_Address)
VALUES
('Dave','Frank','Company1','0115 999999','zvv@zz.com'),
('Dave','Blogs','Company2','0115 888888','zvv@zz.com'),
('David','frank','Company3','0115 777777','zvv@zz.com')

Error: 1
INSERT INTO contacts_old (First_Name, Last_Name, Company, Business_Phone, Email_Address) VALUES ('Dave','Frank','Company1','0115 999999','zvv@zz.com'), ('Dave','Blogs','Company2','0115 888888','zvv@zz.com'), ('David','frank','Company3','0115 777777','zvv@zz.com')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

This is just a framework to show the idea of building the query based on the data in the JSON. It would require some tweaking and adjusting based on your actual data and table schemas.

As a refinement, I would also build a single INSERT for each table, with multiple values.

EDIT

Changed the code to read and insert all tables in JSON file. Presumes that table names and column names in the JSON file match with the actual tables. It also adds the ON DUPLICATE KEY UPDATE for tables that have an id column.

NOTE: Not tested, may have typos.

<?php
    try
    {
        $connect = mysqli_connect("localhost", "fmart", "password", "mart_dbsync"); 
        $query = '';
        $table_data = '';
        $filename = "single_table.json";

        $data = file_get_contents($filename);
        $array = json_decode($data, true); 

        foreach($array as $set) 
        {
            $tblName = $set['tableName'];
            if(sizeof($set['rows']) > 0) {
                $query = '';
                $colList = array();
                $valList = array();
                //  Get list of column names
                foreach($set['rows'][0] as $colname => $dataval) {
                    $colList[] = "`".$colName."`";
                }
                $query .= "INSERT INTO `".$tblName."` \n";
                $query .= "(".implode(",",$colList).")\nVALUES\n";
                //  Go through the rows for this table.
                foreach($set['rows'] as $idx => $row) {
                    $colDataA = array();
                    //  Get the data values for this row.
                    foreach($row as $colName => $colData) {
                        $colDataA[] = "'".$colData."'";
                    }
                    $valList[] = "(".implode(",",$colDataA).")";
                }
                //  Add values to the query.
                $query .= implode(",\n",$valList)."\n";
                //  If id column present, add ON DUPLICATE KEY UPDATE clause
                if(in_array("`id`",$colList)) {
                    $query .= "ON DUPLICATE KEY UPDATE\n\tSet ";
                    $tmp = array();
                    foreach($colList as $idx => $colName) {
                        $tmp[] = $colName." = new.".$colname." ";
                    }
                    $query .= implode(",",$tmp)."\n";
                } else {
                    echo "<p><b>`id`</b> column not found. <i>ON DUPLICATE KEY UPDATE</i> clause <b>NOT</b> added.</p>\n";
                    echo "<p>Columns Found:<pre>".print_r($colList,true)."</pre></p>\n";
                }
                echo "<p>Insert query:<pre>$query</pre></p>";
                $r = mysqli_query($connect, $query);  
                echo "<h1>".mysqli_num_rows($r)." Rows appeded in $tblName</h1>";
            } else {
                echo "<p>No rows to insert for $tblName</p>";
            }
        }
    } 

    catch(Exception $e)
    {   
        echo $e->getMessage();  
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM