最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP接收json 并將接收數(shù)據(jù)插入數(shù)據(jù)庫的實(shí)現(xiàn)代碼

 更新時(shí)間:2015年12月01日 12:15:28   作者:Hola  
這篇文章主要介紹了PHP接收json 并將接收數(shù)據(jù)插入數(shù)據(jù)庫的實(shí)現(xiàn)代碼,需要的朋友可以參考下

最近有一個(gè)需求,前端向后臺提交json,后臺解析并且將提交的值插入數(shù)據(jù)庫中,
難點(diǎn)
1、php解析json(這個(gè)不算難點(diǎn)了,網(wǎng)上實(shí)例一抓一大把)
2、解析json后,php怎樣拿到該拿的值

<?php
require ('connect.php');
/*
本例用到的數(shù)據(jù):
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'];

//--解析Json,獲取對應(yīng)的變量值
$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'];

$i=0;//循環(huán)變量

//--得到Json_list數(shù)組長度
$num=count($obj["json_list"]);

//--遍歷數(shù)組,將對應(yīng)信息添加入數(shù)據(jù)庫
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();
}

//--添加訂單信息
$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();
?>

投稿者信息
昵稱: Hola
Email: jamcistos@outlook.com

相關(guān)文章

最新評論

龙游县| 新和县| 广饶县| 湖州市| 龙陵县| 桓台县| 公安县| 扎赉特旗| 蒙山县| 若羌县| 乾安县| 灵山县| 临颍县| 库车县| 湘潭县| 工布江达县| 大邑县| 马鞍山市| 蕲春县| 彰化县| 正宁县| 轮台县| 临沭县| 即墨市| 延边| 岚皋县| 南丹县| 双峰县| 信阳市| 监利县| 金寨县| 色达县| 汾西县| 铅山县| 鹤庆县| 财经| 六枝特区| 梁平县| 英吉沙县| 安龙县| 延川县|