-1
"SCI-6":{"quantity":11,"id":"SCI-6","price":15,"name":"notebooks"},
"SCI-7":{"quantity":1,"id":"SCI-7","price":10,"name":"posters"},
"SCI-8":{"quantity":2,"id":"SCI-8","price":15,"name":"pen"},
"SCI-9":{"quantity":4,"id":"SCI-9","price":100,"name":"charger"},
"SCI-10":{"quantity":1,"id":"SCI-10","price":10.25,"name":"News Paper"}

please help me to decode as variables and insert the values to Mysql using php

naseeba c
  • 1,040
  • 2
  • 14
  • 30

5 Answers5

3

Use json_decode — Decodes a JSON string

Try as below :

<?php 
$json = '{"SCI-6":{"quantity":11,"id":"SCI-6","price":15,"name":"notebooks"},"SCI-7":{"quantity":1,"id":"SCI-7","price":10,"name":"posters"},"SCI-8":{"quantity":2,"id":"SCI-8","price":15,"name":"pen"},"SCI-9":{"quantity":4,"id":"SCI-9","price":100,"name":"charger"},"SCI-10":{"quantity":1,"id":"SCI-10","price":10.25,"name":"News Paper"}}';
$result_data = json_decode($json);
foreach($result_data as $value)
{
    $stmt = mysqli_prepare("INSERT INTO tablename (quantity, id, price, name) (?, ?, ?, ?)");
    mysqli_stmt_bind_param($stmt, "dsss", $value->quantity, $value->id, $value->price, $value->name);
    mysqli_stmt_execute($stmt);
}
?>

Output you will get is :

stdClass Object
(
    [SCI-6] => stdClass Object
        (
            [quantity] => 11
            [id] => SCI-6
            [price] => 15
            [name] => notebooks
        )

    [SCI-7] => stdClass Object
        (
            [quantity] => 1
            [id] => SCI-7
            [price] => 10
            [name] => posters
        )

    [SCI-8] => stdClass Object
        (
            [quantity] => 2
            [id] => SCI-8
            [price] => 15
            [name] => pen
        )

    [SCI-9] => stdClass Object
        (
            [quantity] => 4
            [id] => SCI-9
            [price] => 100
            [name] => charger
        )

    [SCI-10] => stdClass Object
        (
            [quantity] => 1
            [id] => SCI-10
            [price] => 10.25
            [name] => News Paper
        )

)
AnkiiG
  • 3,468
  • 1
  • 17
  • 28
2
$data = '{"SCI-6":{"quantity":11,"id":"SCI-6","price":15,"name":"notebooks"},"SCI-7":{"quantity":1,"id":"SCI-7","price":10,"name":"posters"},"SCI-8":{"quantity":2,"id":"SCI-8","price":15,"name":"pen"},"SCI-9":{"quantity":4,"id":"SCI-9","price":100,"name":"charger"},"SCI-10":{"quantity":1,"id":"SCI-10","price":10.25,"name":"News Paper"}}';

$json_data=json_decode($data);
foreach($json_data as $json_value)
{

    mysql_query("INSERT INTO yourtable (quantity, id, price, name) values ('".$json_value->quantity."','".$json_value->id."','".$json_value->price."','".$json_value->name."')");

}
Aslam Patel
  • 874
  • 6
  • 19
0

Assuming your json string is

$dat = '{"SCI-6":{"quantity":11,"id":"SCI-6","price":15,"name":"notebooks"},"SCI-7":{"quantity":1,"id":"SCI-7","price":10,"name":"posters"},"SCI-8":{"quantity":2,"id":"SCI-8","price":15,"name":"pen"},"SCI-9":{"quantity":4,"id":"SCI-9","price":100,"name":"charger"},"SCI-10":{"quantity":1,"id":"SCI-10","price":10.25,"name":"News Paper"}}';

With the opening and closing brackets at the end (otherwise it's not valid json) then we can decode it like the following:

$jsondata = json_decode($dat, true);
foreach($jsondata as $vals)
{
    $stmt = mysqli_prepare("INSERT INTO yourtable (quantity, id, price, name) (?, ?, ?, ?)");
    mysqli_stmt_bind_param($stmt, "dsss", $vals['quantity'], $vals['id'], $vals['price'], $vals['name']);
    mysqli_stmt_execute($stmt);
}
Matt
  • 2,851
  • 1
  • 13
  • 27
0

You can use json_decode function for decoding json:

$string = '{"SCI-6":{"quantity":11,"id":"SCI-6","price":15,"name":"notebooks"},"SCI-7":{"quantity":1,"id":"SCI-7","price":10,"name":"posters"},"SCI-8":{"quantity":2,"id":"SCI-8","price":15,"name":"pen"},"SCI-9":{"quantity":4,"id":"SCI-9","price":100,"name":"charger"},"SCI-10":{"quantity":1,"id":"SCI-10","price":10.25,"name":"News Paper"}}';

echo "<pre>";    
print_r(json_decode($string));

Result:

stdClass Object
(
    [SCI-6] => stdClass Object
        (
            [quantity] => 11
            [id] => SCI-6
            [price] => 15
            [name] => notebooks
        )

    [SCI-7] => stdClass Object
        (
            [quantity] => 1
            [id] => SCI-7
            [price] => 10
            [name] => posters
        )

    [SCI-8] => stdClass Object
        (
            [quantity] => 2
            [id] => SCI-8
            [price] => 15
            [name] => pen
        )

    [SCI-9] => stdClass Object
        (
            [quantity] => 4
            [id] => SCI-9
            [price] => 100
            [name] => charger
        )

    [SCI-10] => stdClass Object
        (
            [quantity] => 1
            [id] => SCI-10
            [price] => 10.25
            [name] => News Paper
        )

)

Side Note:

Your json consists on multidimensional array so you need to use json inside the {}.

devpro
  • 16,184
  • 3
  • 27
  • 38
-1

You can use json_decode() to decode the json data.

You can also try unserialize() if it doesn't works.

Wolfack
  • 2,667
  • 1
  • 26
  • 50