php实现购物车数量更新的代码一例

发布时间:2019-10-08编辑:脚本学堂
本文介绍下在php中实现购物车数量管理的代码,根据用户的选择商品加入而不断增加的,如何添加与删除,是购物车实现的关键,有需要的朋友,可以参照下本文介绍的方法。

本文内容共分为二个部分,一是html表单,二是php处理部分。

1、表单部分:

<form action="?action=edit_num" method="post" name="car<?php $c_rs['id'];?>" id="car<?php $c_rs['id'];?>">
<input name="suliang[<?php echo $c_rs['sp_id'];?>]" type="text" id="suliang[<?php echo $c_rs['sp_id'];?>]" value="<?php echo $c_rs['suliang'];?>"/>
<input type="submit" name="button" id="button" value="更新购物车" />
</form>

2、php处理代码
 

复制代码 代码示例:

<?php
/**
 * php购物车 处理代码
 * Edit www.jb200.com
*/
require 'config.inc.php'; //配置文件
require 'checklogin.php'; //登录检测
$username = $_SESSION['username'];
$action = $_GET['action'];
switch ($action) {
case "edit_num":

$arr = $arr = $_POST['suliang'];
foreach($arr as $key=>$value){
$sqlgx = "update `cartemp` set suliang='$value' where username='".$username."' and flag=0 and sp_id='".$key."'";
mysql_query($sqlgx, $conn);
echo "<script>location.href='shopcat.php'</script>";
}
break;

case "null":
$null_sql = "delete from `cartemp` where username='$username' and flag=0 ";
mysql_query($null_sql, $conn);
echo "<script>location.href='shopcat.php'</script>";
break;
case "del":
$id = $_GET['id'];
$del_sql = "delete from `cartemp` where id=$id";
mysql_query($del_sql, $conn);
echo "<script>location.href='shopcat.php'</script>";
break;
}
?>