Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to insert a JSON array into my MySQL database. Here is the format of the array:

${
    "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"
}

I tried inserting the query as below:

 <?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());
        }
?>

Also I decode the JSON data, and foreach loop.

Kindly help me in this issue. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

May be you should try to normalize your database. Since a row of the order table is representing an order, and you can't show arbitrary amount of products in a column(unless you just stringify the array and put it in.)


Your JSON appear to be an order. And intuitively, an order is ordered by a customer. On the other hand, a customer could have many orders. So the customer-to-order relation suppose to be a one-to-many relation, which shall be implemented by a foreign key customer_id from order to customer

Followed up by products and orders relation. A product can be shown in many orders. Also, an order could contain many products. Therefore, the product-to-order relation shall be a many-to-many relation. Empirically, you should have another table to maintain the relation. Let say the table order_product have two foreign keys order_id and product_id point to order and product tables, respectively. In addition, this table should have another column stores product quantity.


I have seen your update, there are some errors in your code. Since 'products' is an array, retrieving product id and product quantity shall like below

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...