2

I have no idea about PHP can anyone help me how can I store nested JSON data into the database? I write code but it is for a single row at a time but If I want to parse nested JSON how can I?

[
        {
        "product_name": "Product3",
        "product_price": "500",
        "product_quantity": "5",
        "userid": "1",
        "orderid": "1"
    },
        {
        "product_name": "Product4",
        "product_price": "500",
        "product_quantity": "5",
        "userid": "1",
        "orderid": "1"
    },
        {
        "product_name": "Product5",
        "product_price": "500",
        "product_quantity": "5",
        "userid": "1",
        "orderid": "1"
    }

]
// Creating MySQL connection.
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

// Storing the received JSON in $json.
$json = file_get_contents('php://input');

// Decode the received JSON and store into $obj
$obj = json_decode($json,true);



$productid = $obj['product_id'];

$productname = $obj['product_name'];

$productprice = $obj['product_price'];

$productquantity = $obj['product_quantity'];

$userid = $obj['userid'];

$orderid = $obj['orderid'];



$query = "INSERT INTO order_product (product_id, product_name, product_price, product_quantity, userid, orderid) VALUES ('$productid','$productname','$productprice','$productquantity','$userid','$orderid')";

if(mysqli_query($con,$query)){

     // On query success it will print below message.
    $MSG = 'Data Successfully Submitted.' ;

    // Converting the message into JSON format.
    $json = json_encode($MSG);

    // Echo the message.
     echo $json ;

 }
 else{

    echo 'Try Again';

 }

I am trying to insert using Postman but only a single blank row inserted. Help me please how can store into the database after hit API?

1
  • 2
    Add a loop, and do what you are currently doing for a single object inside of that loop … Commented Jan 28, 2020 at 13:18

1 Answer 1

2

Use the loop to insert your json value. I haven't tested but assume it will work for you

// Creating MySQL connection.
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

// Storing the received JSON in $json.
$json = file_get_contents('php://input');

// Decode the received JSON and store into $obj
$obj = json_decode($json,true);

foreach($obj as $product) : 

  $productid = $product['product_id'];
  $productname = $product['product_name'];
  $productprice = $product['product_price'];
  $productquantity = $product['product_quantity'];
  $userid = $product['userid'];
  $orderid = $product['orderid'];

  if(!empty($productid)) :
    $query = "INSERT INTO order_product (product_id, product_name, product_price, 
   product_quantity, userid, orderid) VALUES 
('$productid','$productname','$productprice','$productquantity','$userid','$orderid')";

    if(mysqli_query($con,$query)){

       // On query success it will print below message.
      $MSG = 'Data Successfully Submitted.' ;

      // Converting the message into JSON format.
      $json = json_encode($MSG);

      // Echo the message.
     echo $json ;

    }
    else{

     echo 'Try Again';

    }
  endif;
endforeach;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.