代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
echo "添加购物车的处理过程";
$userId=$_COOKIE['nhuid'];
$cpid=$_POST['cpid'];
$cnum=$_POST['cnum'];
//echo "<br>$userId $cpid $cnum";
//1、连接数据库
$link= mysqli_connect("localhost","root","");
mysqli_select_db($link, "nhcar");
mysqli_set_charset($link,"utf8");
//2、操作数据表
$sql1 ="SELECT * FROM cart WHERE cuid=$userId AND cpid=$cpid;";
$result1= mysqli_query($link, $sql1);
$row1= mysqli_fetch_assoc($result1);
$result=false;//默认插入或更新失败
if ($row1){
//如果查询购物车有已购买的商品,那么就更新商品数量
$sql2="UPDATE cart SET cnum=cnum+$cnum WHERE cuid=$userId AND cpid=$cpid;";
echo "<br>$sql2<br>";
$result= mysqli_query($link, $sql2);
}else{
$sql="INSERT INTO cart(cuid,cpid,cnum) VALUE ($userId,$cpid,$cnum);";
echo "<br>$sql2<br>";
$result= mysqli_query($link, $sql);
}
//3、处理结果数据
if ($result) {
echo "<script>alert('添加购物车成功!');location.href='cart.php'</script>";
} else {
echo "<script>alert('添加购物车失败!');location.href='productlist.php'</script>";
}
?>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。