JSON解码到MYSQL数据库

I am having JSON POST like this.

{"item_sku":["016","APL","017"],"item_quantity":[2,4,1]}

I want to insert it into mysql database table and in column named 'item_sku' and 'item_quantity'. I guess this will insert three rows.

HTML Page:

  <form id="form1" class="p-md col-md-6" ng-submit="addOrder()">

  <ul class="list-group m-b-sm md-whiteframe-z0" ng-repeat="item in cart.items">
    <li class="list-group-item">
      <a ui-sref="app.product({productSku: item.sku})" class="pull-right w-56"><img ng-src="http://192.168.0.228/app/{{ item.image || 'img/default_product.jpg' }}" alt="{{item.sku}}" class="img-responsive"></a>
      <div class="clear">
        <b>{{item.name}}</b><br>
        <span style="color:#666;font-size:12px;">{{item.description}}</span><br>
        <span style="color:#666;font-size:12px;">SKU# {{item.sku}}</span>
        <input type="text" ng-model="sku[$index]" ng-init="sku[$index]=item.sku">
        <br><br>            
      </div>
      <div>
        <!-- use type=tel instead of  to prevent spinners -->
        <button
            class="form-control btn btn-grey" type="button" style="width:34px;background-color:#ddd;"
            ng-disabled="item.quantity <= 1"
            ng-click="cart.addItem(item.sku, item.name, item.price, -1)">-</button>
        <input
            class="form-control btn btn-grey" size="2" style="width:44px" type="tel"
            ng-model="item.quantity"
            ng-change="cart.saveItems()" />
        <button
            class="form-control btn btn-grey" type="button" style="width:34px;background-color:#ddd;"
            ng-disabled="item.quantity >= 100"
            ng-click="cart.addItem(item.sku, item.name, item.price, +1, $index)" >+</button>
            <input type="text" ng-model="quantity[$index]" ng-init="quantity[$index]=item.quantity">
      </div>    
    </li>

        <div class="p b-t b-t-2x">
            <a style="color:#666;" ng-click="cart.addItem(item.sku, item.name, item.price, -10000000)" >
            <i class="icon mdi-action-delete i-20" style="color:#666;"></i>Remove</a> 
            <span class="pull-right">{{item.price * item.quantity | currency:' &#8377; ':2}}</span>
        </div>  

  </ul>                 


   <button type="submit" class="btn btn-info m-t" >Submit</button>
  </form>

Controller:

app.controller('OrderCtrl', function($scope, $location, $http ) {
$scope.sku = [];
$scope.quantity = [];
    $scope.addOrder = function(){
        $http.post(serviceURL+'submit_order.php', {
                        'item_sku':$scope.sku,
                        'item_quantity':$scope.quantity
        })

        .success(function(data,status,headers,config){
        $location.path('app/feedbackthankyou');
        });
    };  
});

PHP Code:

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");

include "db.php";
// Using Static Data from POST -- Just for testing
$a='{"item_sku":["016","APL","017"],"item_quantity":[2,4,1]}';
$data = json_decode($a);
var_dump($data);

    foreach ($data as  $value)
    {
        echo $value->item_sku;
        echo $value->item_quantity;
    }
?> 

I guess details are enough now. BTW I am using AngularJS to create CART. Please help.

The shape of your data complicates the database insertion a bit. Data that should go in the same row is divided into two separate objects: $data->item_sku and $data->item_quantity.

It would be simpler if everything that should go in one row were together in one variable or one array element. I would change the shape of the data I submit. Instead of a item_sku and item_quantity, I would just submit items.

Javascript

var items=[];
for(var i=0; i<$scope.sku.length; i++){
   items.push({'sku':$scope.sku[i], 'qty':$scope.quantity[i]});
}
$http.post(serviceURL+'submit_order.php', {items:items})

Then on the PHP side

$data = json_decode($_POST['items'],true);

Now $data is an array of items with the shape:

[
    ['sku'=>'016','qty'=>2],
    ['sku'=>'APL','qty'=>4],
];

This can be more easily inserted in your database because the data is already organized in rows. You said you already know how to do DB inserts so I'll let you work out that part.