将嵌套的json数组插入mysql

时间:2016-01-13 10:11:36

标签: php mysql arrays json

我正在尝试将JSON数组插入MySQL数据库。这是数组的格式:

${
    "customer_id": "1",
    "products":[ {
        "product_id": "1",
        "product_qty": "2"
    }, {
        "product_id": "2",
        "product_qty": "4"
    }, {
        "product_id": "3",
        "product_qty": "12"
    }, {
        "product_id": "4",
        "product_qty": "22"
    }],
    "order_totalamount": "100"
}

我尝试插入如下查询:

 <?php
   require("config.inc.php");
   $jsondata = file_get_contents('OrderFormat.json');
    //convert json object to php associative array
    $data = json_decode($jsondata, true);

   //get the employee details
   $cus_id = $data['customer_id'];
   $product_id = $data['products']['product_id'];
   $product_qty = $data['products']['product_qty'];
   $order_totalamount = $data['order_totalamount'];

        //insert into mysql table
         $sql = "insert into `order`(cm_id,product_id,product_quantity,order_totalamount,order_id,order_date) values ($cus_id,$product_id,$product_qty,$order_totalamount,$cus_id,CURDATE())";

        echo $sql;
        //$sql1 = mysql_query($sql);
        $conn = mysqli_connect($host, $username, $password, $dbname);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }

        if(!mysqli_query($conn,$sql))
        {
            die('Error : ' . mysql_error());
        }
?>

我也解码JSON数据,并解析循环。

请在这个问题上帮助我。 提前谢谢。

5 个答案:

答案 0 :(得分:1)

可能是您应该尝试规范化您的数据库。由于order表的一行表示订单,并且您无法在列中显示任意数量的产品(除非您只是对数组进行字符串化并将其放入。)

您的JSON似乎是一个订单。直观地说,订单是由客户订购的。另一方面,客户可能有很多订单。因此,客户 - 订单关系假设为一对多关系,应由customer_idorder的外键customer实现

跟进产品和订单关系。产品可以以多个订单显示。此外,订单可能包含许多产品。因此,产品与订单的关系应是多对多关系。根据经验,你应该有另一个表来维持这种关系。假设表order_product有两个外键order_idproduct_id分别指向orderproduct表。此外,此表应该有另一列存储产品数量。

我看到了您的更新,您的代码中存在一些错误。由于'products'是一个数组,因此检索产品ID和产品数量应如下所示

$product_id = $data['products'][$i]['product_id'];
$product_qty = $data['products'][$i]['product_qty'];

答案 1 :(得分:0)

尝试如下:

<?php
//require("config.inc.php");
$jsondata ='{
"customer_id": "1",
"products":[ {
"product_id": "1",
"product_qty": "2"
}, {
"product_id": "2",
"product_qty": "4"
}, {
"product_id": "3",
"product_qty": "12"
}, {
"product_id": "4",
"product_qty": "22"
}],
"order_totalamount": "100"
}';

//convert json object to php associative array
$data = json_decode($jsondata, true);

//get the employee details
$cus_id = $data['customer_id'];
$order_totalamount = $data['order_totalamount'];
$order_totalamount = $data['order_totalamount'];

foreach($data['products'] as $key => $val)
{
    $product_id = $val['product_id'];
    $product_qty = $val['product_qty'];

    //insert into mysql table
    $sql = "insert into `order`(cm_id,product_id,product_quantity,order_totalamount,order_id,order_date) values ($cus_id,$product_id,$product_qty,$order_totalamount,$cus_id,CURDATE())";

    echo $sql;
    //$sql1 = mysql_query($sql);
    $conn = mysqli_connect($host, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    if(!mysqli_query($conn,$sql))
    {
        die('Error : ' . mysql_error());
    }
}
?>

答案 2 :(得分:0)

试用此代码

R = {0,1,2,3,4,5}, S = 5, K = 2
Solutions: randomly pick one of {{1,4};{2,3};{0.5}}

答案 3 :(得分:0)

<?php
$json = '{
    "customer_id": "1",
    "products":[ {
        "product_id": "1",
        "product_qty": "2"
    }, {
        "product_id": "2",
        "product_qty": "4"
    }, {
        "product_id": "3",
        "product_qty": "12"
    }, {
        "product_id": "4",
        "product_qty": "22"
    }],
    "order_totalamount": "100"
}';

$obj = json_decode($json);
$data=$obj->{'products'};
foreach($data as $item){
$sql = "insert into `order`(cm_id,product_id,product_quantity,order_totalamount,order_id,order_date) values ($cus_id,".$item->{'product_id'}.",".$item->{'product_qty'}.",$order_totalamount,$cus_id,CURDATE())";

}


?>

答案 4 :(得分:-1)

试试这段代码

crontab