title: How do I insert multiple rows of JSON object data in mySQL using PHP PDO?
body: <p>I making PHP API to insert multiple rows of <strong>JSON</strong> object data. My <strong>JSON data format</strong> is like this:</p>
  
  <p><em>(I receive this below format in console log for console.log(this.state.cartItems[]).</em>  in REACT)</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>0: {SparePartID: "34", qty: 1, Price: "500", OrderID: "14"}
  1: {SparePartID: "35", qty: 1, Price: "250", OrderID: "14"}
  2: {SparePartID: "36", qty: 1, Price: "430", OrderID: "14"}</code></pre>
  </div>
  </div>
  </p>
  
  <p>My PHP Api code is below:</p>
  
  <p>---> <strong>part_order_details.php</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;?php
  
  header("Access-Control-Allow-Origin: http://localhost/Auth/");
  header("Content-Type: application/json; charset=UTF-8");
  header("Access-Control-Allow-Methods: POST");
  header("Access-Control-Allow-Origin: *");
  header("Access-Control-Max-Age: 3600");
  header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  
  include_once '../config/database.php';
  include_once '../objects/order.php';
  
  
  $database = new Database();
  $db = $database-&gt;getConnection();
  
  
  $order = new Order($db);
  
  $a = file_get_contents("php://input");
  $data = json_decode($a, true);
  
  if($order-&gt;orderDetails($data)){
  
      http_response_code(200);
      echo json_encode(array(
          "message" =&gt; "All rows of Order Details are inserted.",
  
      ));
  }
  else{
  
      http_response_code(400);
      echo json_encode(array("message" =&gt; "Sorry! Error while inserting rows of order details"));
  }
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>and -----> <strong>order.php</strong> is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;?php
  class Order{
  
      private $conn;
  
      public $SparePartID;
      public $OrderID;
      public $Price;
      public $Quantity;
  
      public function __construct($db){
          $this-&gt;conn = $db;
      }
  
  
      function orderDetails($arr)
      {
         $query=  "INSERT INTO sparepartorderdetails (SparePartID, OrderID, Quantity, Price) VALUES
        (:SparePartID, :OrderID, :qty, :Price) ";
        
          $stmt = $this-&gt;conn-&gt;prepare($query);
  
          foreach($arr as $item)
          {
              $stmt-&gt;bindValue(':SparePartID', $item[0]);
              $stmt-&gt;bindValue(':qty', $item[1]);
              $stmt-&gt;bindValue(':Price', $item[2]);
              $stmt-&gt;bindValue(':OrderID', $item[3]);
  
            if($stmt-&gt;execute()){
              return true;
          }
          else{
             $arr = $stmt-&gt;errorInfo();
              print_r($arr);
          }
          }
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>For now, Im trying to test PHP API with <strong>POSTMAN</strong>.
   so Im sending this data in Postman body for <strong>POST</strong> request:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>{
  "0":
  {"SparePartID": "34",
  "qty": "1",
  "Price": "500",
  "OrderID": "14"},
  
  "1":
  {"SparePartID": "35",
  "qty": "1",
  "Price": "250",
  "OrderID": "14"}
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>But the POSTMAN shows <strong>ERROR</strong> Status: <em>400 Bad Request</em> with msg: 
  {
      "message": <strong>"Sorry! Error while inserting rows of order details"</strong>
  }</p>
  
  <p>I searched this problem alot but no solution.
   Am I missing something or using wrong way to insert mutiple JSON rows? Please Help me! </p>