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.

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

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名