您好,登錄后才能下訂單哦!
1.提交數(shù)據(jù):
<a href="javascript:addCate(<?php echo $data['id']?>);">提交</a>
<script type=text/javascript>
function addCate(productid)
{
//ajax請求php腳本完成數(shù)據(jù)的添加購物車表中
var url=ddCate.php;//提交地址
//確定需要傳遞的值,產(chǎn)品id 數(shù)量,數(shù)量根據(jù)輸入框id得到并轉(zhuǎn)換為整數(shù)
var data={"productid":productid,"num":parseInt($('#number').val())}
//第三個參數(shù) 回掉函數(shù)
var success=function(response)
{
if(response.errno==0){
alert('加入購物車成功');
}else{
alert('加入購物車失?。?;
}
}
$.post(url,data,success,"json");
}
</script>
2.創(chuàng)建addCate.php表單加入購物車操作
//01.接收傳遞過來的參數(shù)
$productid=intval($_POST['productid']);//接受的為數(shù)據(jù) 可以進行×××約束
$num=intval($_POST['num']);
//02.準備要添加購物車數(shù)據(jù)
session_start();
$userid=$_SESSION['userid'];//得到用戶id
//根據(jù)產(chǎn)品id 進行數(shù)據(jù)庫查詢 得到商品單價
try{
$pdo=new PDO("mysql:host=localhost;dbname=username","root","root",array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
$pdo->query('set names utf8);
$sql="select price from shop_product where id=?";
$stmt=$pdo->prepare($sql);
$stmt->execute(array($productid));
$data=$stmt->fetch(PDO::FETCH_ASSOC);
$price=$data['price'];
$createtime=time();
//03.完成購物車數(shù)據(jù)添加,進行預處理
$sql="insert into shop_cart(productid num userid price createtime)valur(?,?,?,?,?)";
$stmt=$pdo->prepare($sql);
$stmt->execute(array($product,$num,$price,$userid,$price,$createtime));
$rows=$stmt->rowcount();
}catch(PDO::Exception $e)
{
echo $e->getMessage();
}
//04.返回最后添加的結果
if($rows){
$response=array(
'errno'=>0;
'errmsg'=>'success’,
‘data'=>true,
)
}else{
$response=array(
'errno'=>-1;
'errmsg'=>'fail’,
‘data'=>fals,
)
}
echo json_encode($response);
3.數(shù)據(jù)表
shop_cart:
id (bigint(20)),product_id(bigint(20)), userid(bigint(20)), num(int(10)默認1) ,price(float8,2), createtime(int(10)).
shop_product:
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。