doufu9145 2016-07-16 11:36 采纳率: 0%
浏览 67
已采纳

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.

  • 写回答

1条回答 默认 最新

  • doukuangxiu1621 2016-07-16 13:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable