There is a requirement recently that the front end submits JSON to the background, parses the background and inserts the submitted value into the database,
difficulty
1, PHP parsing JSON (this is not a difficult problem, a bunch of online instances)
2, after parsing json, how does PHP get the value of the fetch
<?php require (' connect.php '); /* This example uses the data: post_array={"order_id": "0022015112305010013", "buyer_id": "2", "seller_id": "1", "All_price": "100.00", " Json_list ": [{" product_id ":" 3 "," Product_number ":" 3 "},{" product_id ":" 8 "," Product_number ":" 2 "},{" product_id ":" 10 "
, "Product_number": "4"}]} * * * $post _array=$_post[' Post_array '];
--parse JSON to get the corresponding variable value $obj =json_decode ($post _array,true);
$order _id = $obj [' order_id '];
$buyer _id = $obj [' buyer_id '];
$seller _id = $obj [' seller_id '];
$all _price = $obj [' All_price '];
The length $num =count ($obj ["json_list"]) of the json_list array is obtained $i =0;//cyclic variable//--;
--Iterate over the array and add the corresponding information to the database for ($i; $i < $num; $i + +) {$list _product_id[]= $obj ["Json_list"] [$i] ["product_id"];
$list _product_number[]= $obj ["Json_list"] [$i] ["Product_number"];
$insert _order_product_sql= "INSERT into Tbl_order_product (order_id,product_id,product_number) VALUES (?,?,?)";
$result = $sqlconn-> Prepare ($insert _order_product_sql);
$result-> Bind_param ("SSS", $order _id, $list _product_id[$i], $list _product_number[$i]); $result->execute ();
//--Add order information $insert _order_sql= "INSERT into Tbl_order (order_id,buyer_id,seller_id,all_price) VALUES (?,?,?,?)";
$result = $sqlconn->prepare ($insert _order_sql);
$result->bind_param ("SSSs", $order _id, $buyer _id, $seller _id, $all _price);
$result->execute ();
$result-> Close (); $sqlconn-> Close ();?>
Contributor Information
Nickname: Hola
email:jamcistos@outlook.com