PHP to receive JSON and insert received data into the database implementation code _php tips

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.