Insert JSON data into MySQL using PHP

how to insert multiple json data into mysql database in php
how to insert json data into mysql using codeigniter
insert json string into mysql
how to fetch data from database using json in php
how to get data from two mysql table in json using php
how to retrieve data from database in json format using javascript
how to insert json data into mysql using laravel
json data insert mysql php

I tried to insert it to MySQL Base but its not work for me, please help to solve this. This is how JSON looks like:

{
    "status": 1,
    "response": {
        "Aviator Goggles": {
        "price": 1009,
        "quantity": 269
    },
    "Aviator Sunglasses": {
        "price": 460,
        "quantity": 187
    },
    "BIKER CRATE": {
        "price": 29,
        "quantity": 3569
    }
 },
    "time": 1524852778
}

My php code to parse and insert the data:

<?php
$jsonurl = "https://api.opskins.com/IPricing/GetAllLowestListPrices/v1/?appid=578080&format=json_pretty";
$json = file_get_contents($jsonurl);
$data = json_decode($json, true);
print_r ($data);

mysql_connect("127.0.0.1", "root", "") or die (mysql_error ());
mysql_select_db("pubg") or die(mysql_error());

foreach($data as $item) {

       mysql_query("INSERT INTO `c5f` (response, price, quantity) 
       VALUES ('".$item['response']."','".$item['price']."','".$item['quantity']."')") or die(mysql_error());
     }
?>

If you look at the decoded json (print_r($data)), you will see this:

Array
(
    [status] => 1
    [response] => Array
        (
            [Aviator Goggles] => Array
                (
                    [price] => 1009
                    [quantity] => 269
                )

            [Aviator Sunglasses] => Array
                (
                    [price] => 460
                    [quantity] => 187
                )

            [BIKER CRATE] => Array
                (
                    [price] => 29
                    [quantity] => 3569
                )

        )

    [time] => 1524852778
)

To do your database insert (I am replicating your mysql calls here but you really should upgrade, at the very least to MySQLI but in my opinion PDO is better and also to using prepared statements), you need to iterate through $data['response']:

foreach ($data['response'] as $key => $item) {
    mysql_query("INSERT INTO `c5f` (response, price, quantity) 
        VALUES ('$key','{$item['price']}','{$item['quantity']}')") or die(mysql_error());
}

Fetch JSON Data & Insert into Mysql table in PHP, I will show you how to get first JSON Data from external json data file and then after insert Duration: 5:52 Posted: Apr 21, 2016 How to insert JSON data into MySQL database using PHP Database Configuration (db.php) Change database name, username, Database Table. JSON Data (student_data.json) Some sample JSON data on which we will perform our test. PHP – Insert JSON Data into MySQL Database (index.php) Please try

Use PDO for inserting data into your database; here is some example code:

// Set these to your login data
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'pubg');

// We connect to the database using the values above
$pdo = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME, DB_USER, DB_PASS);
// We tell PDO to report us every error
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Now we prepare a query
$sql = $pdo->prepare('INSERT INTO `c5f` SET `response` = :response, `price` = :price, `quantity` = :quantity;');

// We can use $sql to insert data
$data = $response['data'];
foreach($data as $key => $item)
    $sql->execute(array(
        ':response' => $key,
        ':price'    => $item['price'],
        ':quantity' => $item['quantity']
    ));
    echo 'Insert: '. htmlentities($key) .' ('. $item['quantity'] .' - '. $item['price'] .')'."\r\n";
}

This will connect to the database, prepare your insert statement and execute it for every dataset.

How to insert json array into mysql database, $json = file_get_contents('php://input'); $obj = json_decode($json,true);. I think you are passing the wrong variable. You should pass $json in  The UI bundles data as JSON and posts it to PHP through axios, and PHP in turn will decode the JSON and inserts into MySQL. Here is my PHP code (omitting the other lines like connecting etc.):

because of your query is like ...VALUES("response")

but your inner text contain " character

it will go crazy like ...VALUES("{ "price": 460, "quantity": 187 },")


you should escape all inner texts using mysql_real_escape_string function :

"... VALUES ('".mysql_real_escape_string($item['response'])."', ..."

have good time

Insert JSON data into MySQL using php, Insert JSON data into MySQL using php - In this tutorial I will show you How to Insert JSON Data into MySQL using PHP functions like file get_contents,  Insert JSON data into MySQL using php - In this tutorial I will show you How to Insert JSON Data into MySQL using PHP functions like file get_contents, json_decode and mysql query.

How to insert JSON data into MySQL database using PHP, In this tutorial, I will guide you through a step by step process about how to insert JSON data into MySQL database using PHP. The following  Step 3: Decode JSON data to PHP Associative Array. Once the JSON file data is read and available in the form of string, then it becomes easy to decode JSON data into array. For this, the PHP built-in function “json_decode()” function is used which converts the data present in string format to PHP associative array.

Insert JSON Data (from file) into MySQL using PHP (by Valli Pandy , Insert JSON Data (from file) into MySQL using PHP. Author: Valli Pandy. http://​www.kodingmadesimple.com/2014/12/how-to-insert-json-data-into-mysql-php.​html. Insert Data Into MySQL Using MySQLi and PDO. After a database and a table have been created, we can start adding data in them. Here are some syntax rules to follow: The SQL query must be quoted in PHP. String values inside the SQL query must be quoted. Numeric values must not be quoted. The word NULL must not be quoted.

How to Insert JSON Data into MySQL using PHP, How to Insert JSON Data into MySQL using PHP. Hi, in this PHP TUTORIAL, we'll see How to insert JSON Data into MySQL using PHP  Prevent SQL Injection vulnerability. Using the mysqli library is a step ahead from the (soon to be absolutely deprecated) mysql one, and you can and should make use of prepared statements as they will offer a convenient layer for evading SQL injection attacks (without deeply elaborating on this, this is the practice of inserting malicious code

Comments