简体   繁体   中英

Insert Data from a JSON to MySQL database table

I am trying to insert data to 2 different tables in my database.

First table is named business which I have done it to insert data to from the json file to the database.

But when I am trying to insert data into table named business_phone it does not do anything.

Here is my code for inserting the data:

$query = '';
    $query_phones='';
    $table_data = '';
    $filename = "businesses.json";
    $businesses = file_get_contents($filename);
    $business = json_decode($businesses, true);
    foreach($business as $row)
    {
        $query .= "INSERT INTO business(title, address, website, page) VALUES ('".$row["title"]."', '".$row["address"]."', '".$row["website"]."', '".$row["page"]."'); ";
        //data that i will show on page
        $table_data .= '
            <tr>
       <td>'.$row["title"].'</td>
       <td>'.$row["address"].'</td>
       <td>'.$row["website"].'</td>
       <td>'.$row["page"].'</td>
      </tr>
           ';
    }
    foreach($business as $row)
    {
        $query_phones .="INSERT INTO business_phones(business_title, phone_number, phone_name) VALUES ('".$row["title"]."', '".$row["number"]."', '".$row["name"]."');";
    }

Here is some code from the json file

[
    {
        "title": "CONSERVE IT LTD",
        "address": "12 Truman Ave (10) ",
        "phones": [
            {
                "name": "telephone_1",
                "number": "876-754-0220"
            },
            {
                "name": "telephone_2",
                "number": "876-754-0221"
            }
        ],
        "website": "www.conserveitja.com",
        "page": 1
    },
    {
        "title": "Consie Walters Cancer Hospital",
        "address": "22 Deanery Rd (3) ",
        "phones": [
            {
                "name": "telephone_1",
                "number": "876-930-5016"
            }
        ],
        "page": 1
    },
...
]

I don't know how you to handle within php but you can create an auxiliary table to be populated from the file, and then use JSON_TABLE function for the key values to be inserted into that table, provided you're using MySQL DB ver. 8+ :

INSERT INTO business(title, address, website, page)
SELECT t.* 
  FROM tab
 CROSS JOIN
     JSON_TABLE(jsdata, '$[*]' COLUMNS (
                title VARCHAR(100)  PATH '$.title',
                address VARCHAR(100) PATH '$.address',
                website VARCHAR(100)  PATH '$.website',
                page    VARCHAR(100) PATH '$.page')
     ) t

and

INSERT INTO business_phones(business_title, phone_number, phone_name)
SELECT t.* 
  FROM tab
 CROSS JOIN
     JSON_TABLE(jsdata, '$[*]' COLUMNS (
                business_title VARCHAR(100)  PATH '$.title',
                NESTED PATH '$.phones[*]' COLUMNS (
                phone_number VARCHAR(100) PATH '$.number',
                phone_name VARCHAR(100)  PATH '$.name')
                )
     ) t 

a side Note concatenations for sql statements are vulnerable to Injection for most of the programming languages as @Magnuss Eriksson mentioned.

Demo

$row["number"] is not valid. It will be $row["phones"][0] or $row["phones"][1]. Because according to your data "number" and "name" inside of "phones" which is array. You can neatest loop through "phones"

foreach($row["phones"] as $contact){
    $query .="INSERT INTO business_phones(business_title, phone_number, 
    phone_name) VALUES ('".$row["title"]."', '".contact."', 
    '".$row["name"]."');"
}

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