how to insert multiple json data into mysql database in php? – Best 3 Examples

how to insert multiple json data into mysql database in php? – MySQL INSERT statement: It is used to insert/store data into the database with Example 1. Read the JSON file in PHP, 2 – Convert JSON String into PHP Array, 3 – Extract the Array Values in Variables and last Insert JSON to MySQL Database with PHP Code.

how to insert multiple json data into mysql database in php?

first of all you can Connect PHP to MySQL Database and then Read the JSON file in PHP after that Convert JSON String into PHP Array and then Extract the Array Values & last step to Insert JSON to MySQL Database with PHP(index.php) source Code : how to insert multiple json data into mysql database in php?.

Insert JSON data into MySQL using PHP

index.php

<html>
<head>
<title>Insert JSON Data - www.pakainfo.com</title>
</head>
<body>
<?php 

$membersData = '{
"member":{ "name":"Vishal", "country":"India", "MobileNo":989898989 }
}';  

$link = new mysqli('localhost', 'username', 'password', 'member_list_v1');

$sql = "INSERT INTO member_table ( name, jsondata )
VALUES ('Vishal', '$membersData')";

if ($link->query($sql) === TRUE) {
  echo "Insert your JSON record successfully";
} 
?>
</body>
</html>

Store JSON data in MySQL Database

<html>
<head>
<title>Store JSON Data - www.pakainfo.com</title>
</head>
<body>
<?php 
$membersData = '{
"member":{ "name":"Manjudi", "country":"Pakistan", "MobileNo":2545454 }
}';
  $data = json_decode($membersData);
  $singledata = $data->member->name;
  
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "member_lisr";

$link = new mysqli($servername, $username, $password, $dbname);

$sql = "INSERT INTO member_table ( name, jsondata )
VALUES ('Manjudi', '$singledata')";

if ($link->query($sql) === TRUE) {
  echo "Store data only single successfully";
}  
?>
</body>
</html>

Insert Multiple JSON Data into MySQL Database in Php

index.php

<html>
<head>
<title>Insert Multiple JSON Data</title>
</head>
<body>
<?php 

$membersData = '{
"Customer":{ "name":"Damini", "country":"India", "MobileNo":9898989898 },
"Student":{ "name":"Gussy", "country":"USA", "MobileNo":5656569898}
}';  

$link = new mysqli('localhost', 'username', 'password', 'databasename');

$sql = "INSERT INTO member_table ( name, jsondata )
VALUES ('Multidata', '$membersData')";

if ($link->query($sql) === TRUE) {
  echo "Insert your Multiple Members JSON record successfully";
} 
?>
</body>
</html>

Don’t Miss : How To Store Json Data In Mysql Using Php?

Read Also:  how to add multiple input fields dynamically in javascript and PHP?

HOW TO INSERT JSON DATA INTO MYSQL USING PHP?

simple example to how to store json data in mysql using php?
Step 1: Read the JSON file in PHP

$memberjsondata = file_get_contents('members.json');

Step 2: Convert JSON String into PHP Array

$data = json_decode($memberjsondata, true);

Step 3: Extract the Array Values in Variables

$id = $data['memberId'];
$name = $data['profilebio']['name'];
$age = $data['profilebio']['age'];
$streetlocations = $data['profilebio']['locations']['streetlocations'];
$city = $data['profilebio']['locations']['city'];
$state = $data['profilebio']['locations']['state'];
$postalcode = $data['profilebio']['locations']['postalcode'];

Step 4: Insert JSON to MySQL Database with PHP Code

$sql = "INSERT INTO tbl_members(memberId, name, age, streetlocations, city, state, postalcode)
    VALUES('$id', '$name', '$age', '$streetlocations', '$city', '$state', '$postalcode')";
if(!mysqli_query($con, $sql))
{
    die('Error : ' . mysql_error());
}

members.json

{
    "memberId": "MD00548",
    "profilebio": {
        "name": "Virat Kohali",
        "age": "29",
        "locations": {
            "streetlocations": "8 12th Street",
            "city": "Rajkot",
            "state": "GJ",
            "postalcode": "98560"
        }
    }
}

I hope you get an idea about how to insert multiple json data into mysql database in php?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.