PHP 高手 请进来看下这段购物车代码

PHP 高手 请进来看下这段购物车代码,第1张

$sql="SELECT name,price FROM product WHERE id='$id'"

$id是变量,php中虽然双引号和单引号都能表示字符串,但是不同的是,单引号不能解析变量,也就是说'$aaaa'表示的就是字符串$aaaa,而不会解析字符串!

改为戚消缓桥差:高模

$sql="SELECT name,price FROM product WHERE id=$id"

我也要弄一个这种购物车,

我去写个,贴出来,【嘿嘿,今天上午新写的】。

我懒得新建数据库,用的是我的数据库。

你按照我的改一下就能用了

本人水平有限,高手请指正。

你,大,爷的,虽然不咋地,保证尺握能用

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

经过调试,

//$my->add_cart(45,3,"茶几系列")//新增购物

//$my->updata_cart(13,13,8)//更新购物

//$my->del_cart(12,5,'Guest') //删除一种购物

//$my->empty_cart('Guest') //清空购物车

$ok=$my->get_cart('Guest') //返回购物车

这些都可用

-------------------------------------------------------------------

<?php

class Cart

{

public $totalCost=0 //商品总金额

function cart($host,$usr,$pwd,$db)

{

mysql_connect($host,$usr,$pwd) or die(mysql_error)

mysql_select_db($db) or die(mysql_error)

mysql_query("SET Names GBk")

//只要有人访问,就自动清除一天前所有没付款的订单山蠢;

$sql="delete FROM shopcart WHERE TO_DAYS( NOW( )) - TO_DAYS( ptime ) >=1 and payment=0"

mysql_query($sql)

}

// d出提示

function alter($Str,$Url)

{

echo "<Script language='JavaScript'>alert('".$Str."')</Script>"

echo "<meta http-equiv=refresh content=0URL=".$Url.">"

}

//增加购物;三个参数:pid:产品ID,ptl:产品数量,pcid:产品类别

//查询数据库,是否存在此人在本日内订过本产品

//如果订过,那么陵唯庆数量累加,否则插入一个数据库行

function add_cart($pid,$ptl=1,$pcid)

{

if($ptl>=100 || $ptl<=0)

{

$this->alter("最多买99件,最少1件","index.php")

die()

}

if(!$_SESSION['usr']) { $usr='Guest'}

else{ $usr=$_SESSION['usr']}

$sql="select * from shopcart where pid='".$pid."' and usr='".$usr."' and pcid='".$pcid."'"

$ex=mysql_query($sql)

$ex1=mysql_fetch_array($ex)

if(!$ex1)

{

$sql="select * from product where ID='".$pid."' and class1='".$pcid."'"

$ok=mysql_query($sql)

$rs=mysql_fetch_array($ok)

if($rs)

{

$totalCost= $rs['Price'] * $ptl

$sql="insert into shopcart(usr,pid,pname,ptl,price,pcid,psum,payment) Values("

$sql.="'".$usr."',"

$sql.="'".$rs['ID']."',"

$sql.="'".$rs['Name']."',"

$sql.="'".$ptl."',"

$sql.="'".$rs['Price']."',"

$sql.="'".$rs['Class1']."',"

$sql.="'".$totalCost."',"

$sql.="'0')"

mysql_query($sql) or die(mysql_error())

if($ok) { $this->alter("购物成功","index.php")}

else{ $this->alter("购物失败","index.php")}

}

else

{

$this->alter("不存在的商品,或者参数错误","index.php")

die()

}

}

else

{

$sql="update shopcart set ptl= ptl+1,psum = psum+price where ID='".$ex1['ID']."'"

mysql_query($sql)

$this->alter("更新数量成功","index.php")

}

}

//更新购物车的单个产品的数量;

function updata_cart($cid,$ptl,$pid)

{

if($ptl>=100||$ptl<=0)

{

$this->alter('产品数量不对!','index.php')

die()

}

$sql="select * from shopcart where ID='".$cid."' and pid='".$pid."'"

$ok=mysql_query($sql)

if(!ok) { alter("参数发生错误","index.php")}

else

{

$sql="update shopcart set ptl='".$ptl."',psum=price * '".$ptl."' where ID='".$cid."' and pid='".$pid."'"

$ok=mysql_query($sql)

if(!ok) { $this->alter("更新失败","index.php")}

else{ $this->alter("更新成功","index.php")}

}

}

function del_cart($cid,$pid,$usr)

{

$sql="delete from shopcart where usr='".$usr."' and ID='".$cid."' and pid='".$pid."'"

$ok=mysql_query($sql)

if(!$ok) {$this->alter("删除失败","index.php")}

else {$this->alter("删除成功","index.php")}

}

function empty_cart($usr)

{

$sql="delete from shopcart where usr='".$usr."'"

mysql_query($sql) or die(mysql_error)

}

function get_cart($usr)

{

$sql="select * from shopcart where usr='".$usr."'"

$ok=mysql_query($sql)

return $ok

}

}

$my = new Cart("localhost","root","root","mybbs")

//$my->add_cart(45,3,"茶几系列")

//$my->updata_cart(13,13,8)

//$my->del_cart(12,5,'Guest')

//$my->empty_cart('Guest')

$ok=$my->get_cart('Admin')

echo "usr pid pname ptl price pcid psum payment ptime <br><hr><br>"

while($rs=mysql_fetch_array($ok))

{

echo $rs[1]."->".$rs[2]."->".$rs[3]."->".$rs[4]."->".$rs[5]."->".$rs[6]."->".$rs[7]."->".$rs[8]."->".$rs[9]."<br>"

}

?>

、、、、、、、、、、、、、、、、、SQL、、、、、、、、、、、、、、

CREATE TABLE IF NOT EXISTS `shopcart` (

`ID` int(10) NOT NULL auto_increment,

`usr` varchar(50) NOT NULL,

`pid` int(5) NOT NULL,

`pname` varchar(100) NOT NULL,

`ptl` int(3) NOT NULL,

`price` decimal(50,2) NOT NULL default '0.00',

`pcid` varchar(100) NOT NULL,

`psum` decimal(50,2) NOT NULL default '0.00',

`payment` tinyint(1) NOT NULL,

`ptime` timestamp NOT NULL default CURRENT_TIMESTAMP,

PRIMARY KEY (`ID`)

)

product 里面用的ID CLASS1是

`ID` int(6) NOT NULL auto_increment,

`Class1` varchar(20) NOT NULL,

`Price` int(6) NOT NULL,

首先,购物车的数据库设计:

1. id

2. goods_id 商品ID

3. session_id 购物车ID

4.

goods_sn 商品编码

5. goods_name 商品名称

6. shop_price 商品商城售价

7. goods_price

商品真实售价(与shop_price的区别在于,当打折的时候,shop_price是打折之前商品的售价,而goods_price是打折之后的)

8.

goods_number 数量

9. weight 商品重量

10. goods_attr 商品属吵历运性(如颜色,尺码)

11.

promote_code 促销编码(为商品促销,做的策烂肆略模式识别码)

12. is_promote 促销ID

13. stock_id 库存ID

数据库结构大概是这个样子。当然如果还有其他的需求,比如商品的返利积分,商品是否虚拟产品,商品是否代发货等等,可以另加标示符。这里就不一一举例了。

第二点,我们这个购物车要求是在用户不登陆状态下可以把商品添加到购物车内的,这也是最近非常流行的一种方式,所以在购物车数据库设计里面,就没有加入user_id这个字段,考虑到的是用户未登陆下也可以把商品放入购物车。升梁


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11983353.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存