最近有一個需求,前端向后臺提交json,后臺解析并且將提交的值插入數(shù)據(jù)庫中,
創(chuàng)新互聯(lián)是一家從事企業(yè)網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、網(wǎng)站制作、行業(yè)門戶網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計制作的專業(yè)網(wǎng)站設(shè)計公司,擁有經(jīng)驗豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁設(shè)計人員,具備各種規(guī)模與類型網(wǎng)站建設(shè)的實力,在網(wǎng)站建設(shè)領(lǐng)域樹立了自己獨特的設(shè)計風格。自公司成立以來曾獨立設(shè)計制作的站點近1000家。
難點
1、php解析json(這個不算難點了,網(wǎng)上實例一抓一大把)
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
需要PHP基礎(chǔ)知識和數(shù)據(jù)庫基礎(chǔ)知識。
以SQL為例。使用PHP MySQL 函數(shù)可以編輯數(shù)據(jù)庫。
mysql_connect() 函數(shù)打開MySQL 連接。舉例
?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}// 一些代碼...mysql_close($con);
?
mysql_connect()三個參數(shù)分別是服務(wù)器名,連接賬號,連接密碼。
連接之后,可以使用mysql_select_db()設(shè)置要處理的數(shù)據(jù)庫,后面則是用數(shù)據(jù)庫語句處理數(shù)據(jù)。SQL語法簡介網(wǎng)頁鏈接
$rs
=
mysql_query($sql);
這一段改成:
if(mysql_query($sql)){
echo
"script
language=JavaScriptalert('數(shù)據(jù)庫提交成功!');window.location.href='team.php';/script";
}else{
echo
"插入失敗,錯誤原因是{mysql_error()}";
}
然后根據(jù)錯誤原因解決問題,或者把錯誤原因給大家看看。
如果仍然提示成功,請檢查你的權(quán)限,還有你的mysql數(shù)據(jù)庫Team這個表里的主鍵有沒有重復(fù)?
現(xiàn)在,我們創(chuàng)建一個
HTML
表單,這個表單可把新記錄插入
"Persons"
表。
這是這個
HTML
表單:
123456789101112
htmlbody
form
action="insert.php"
method="post"Firstname:
input
type="text"
name="firstname"
/Lastname:
input
type="text"
name="lastname"
/Age:
input
type="text"
name="age"
/input
type="submit"
//form
/body/html
當用戶點擊上例中
HTML
表單中的提交按鈕時,表單數(shù)據(jù)被發(fā)送到
"insert.php"。"insert.php"
文件連接數(shù)據(jù)庫,并通過
$_POST
變量從表單取回值。然后,mysql_query()
函數(shù)執(zhí)行
INSERT
INTO
語句,一條新的記錄會添加到數(shù)據(jù)庫表中。
當前名稱:php如何插入數(shù)據(jù)庫代碼 php數(shù)據(jù)庫添加數(shù)據(jù)
文章URL:http://www.muchs.cn/article20/dohsdco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、品牌網(wǎng)站建設(shè)、網(wǎng)站維護、網(wǎng)站建設(shè)、網(wǎng)站策劃、標簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)