APP下载

基于PHP+MySQL的网上购物系统的设计与开发

2018-02-13沈蕴梅

计算机时代 2018年12期
关键词:购物车

摘  要: 开发了基于PHP+MySQL的网上购物系统。该系统采用PHP、DR、MySQL数据库等技术开发而成。设计开发了包括用户管理模块、订单管理模块、购物车模块和商品管理模块等功能模块。客户能在前台实现商品信息的浏览查询,登录后能够将商品加入购物车,生成订单,管理员能在后台实现信息的增删改查。该系统界面清晰美观,功能比较完善。

关键词: 购物车; 网上购物; 订单管理; 商品管理

中图分类号:TP311          文献标志码:A     文章编号:1006-8228(2018)12-22-03

Abstract: A PHP+MySQL based online shopping system is developed in this paper. The system is developed by using PHP technology, DR technology and MySQL database technology. The function modules developed include user management module, order management module, shopping cart module and commodity management module. Users can browse and query the information of commodities in the foreground, add goods to the shopping cart and generate orders after login, and administrators can add, delete, modify and check the information in the background. The system has a clear and beautiful interface, and perfect function.

Key words: shopping cart; online shopping; order management; commodity management

0 引言

近年來,随着Internet的迅速崛起,互联网已日益成为收集信息的最佳、最快的渠道,并快速进入传统的流通领域。互联网的跨地域性、可交互性、全天候性使其在与传统媒体行业和传统贸易行业的竞争中具有不可抗拒的优势,因而发展十分迅速。在电子商务逐步兴起的大环境下,越来越多的人开始选择网上购物,网上购物可以由商家直接将商品通过物流送达顾客。网上购物具有省时、省事、省心等优点,让顾客足不出户就可以购买到自己满意的商品[1]。

1 系统相关技术基础

目前开发动态网站的主要技术有ASP.NET、JSP、PHP、ASP等,PHP是一种在服务器端执行的多用途脚本语言。PHP开放源代码且可嵌入到HTML中,尤其适合动态网站的开发,现在被很多的网站编程人员广泛应用[3]。

PHP语言流行的主要原因是它的众多优秀特性:免费开源,自由获取;跨平台;速度;功能性。本系统采用Windows+Apache+MySQL+PHP开发而成。

2 系统分析与设计

网上购物系统分为前台后台两个部分,前台主要包括商品管理模块、购物车管理模块和用户管理模块,实现商品显示、商品搜索、商品推荐、购物车管理、订单管理等功能;后台主要实现对信息的增删改查。

在数据库设计方面,根据该系统的功能,分别设计了用户表、商品表等数据表,本文中,主要实现的是购物车模块,涉及到的是商品表,给出商品表的具体信息,如图1所示。

3 购物车模块实现

在商品显示页面中,单击购买或者放入购物车按钮,就可以将商品信息添加到购物车。要实现该功能分两步[5],分别添加gouwuche1.php和gouwuche2.php页面[4]。

gouwuche1.php主要实现如下功能:判断是否已登录,如果没有登录,则提示先登录后购物;判断该商品是否已在购物车;判断该商品是否还有货;将该商品的id号加入到商品列表中,数量1加入数量列表。具体代码如下所示[2]:

<?php

session_start();

include("conn.php");

if($_SESSION['uname']=="")

{ echo "<script>alert('请先登录后购物');history.back();</script>";

exit; } //判断用户是否登录

$id=$_GET['id'];

$sql=mysql_query("select * from tb_shangpin where

id='".$id."'");

$info=mysql_fetch_array($sql);

if($info['shuliang']<=0)

echo "<script>alert('该商品已售完');history.back()

</script>"; //判断该商品是否有货

$array=explode("@",$_SESSION['producelist']);

for($i=0;$i<count($array)-1;$i++)

{ if(intval($array[$i])==intval($id))

{ echo '<script>alert("该商品已放入购物");

history.back();</script>';

exit; }} //判断商品是否已放入购物车

$_SESSION['producelist']=$_SESSION['producelist'].$id."@";

$_SESSION['quatity']=$_SESSION['quatity']."1@";

header("location:gouwuche2.php?qingkong=no");?>

在gouwuche2.php中能够实现移除商品、修改商品数量和清空购物车功能。代码如下[2]:

<form name="form1" method="post" >

<table width="557" border="0" align="center" >

<tr> <td height="46" background="images/cart.gif"

colspan="6"></td> </tr>

<?php

$_SESSION['total']="";

if($_GET['qingkong']=="yes") {

$_SESSION['producelist']="";

$_SESSION['quatity']=""; } //实现清空购物车功能

include("conn.php");

$arraygwc=explode("@",$_SESSION['producelist']);

$s=0;

for($i=0;$i<count($arraygwc)-1;$i++)

{ $s+=$arraygwc[$i]; }

if($s==0)

{ echo "<tr>";

echo "<td height='25' colspan='6' bgcolor='#FFFFFF'

align='center'>您的購物车为空!</td>";

echo "</tr>"; }  //判断购物车是否为空

else { ?>

<tr>

<td height="25" bgcolor="#FFFFFF">商品名称</td>

<td  bgcolor="#FFFFFF">数量</td>

<td  bgcolor="#FFFFFF">市场价</td>

<td  bgcolor="#FFFFFF">会员价</td>

<td  bgcolor="#FFFFFF">折扣</td>

<td  bgcolor="#FFFFFF">小计</td>

<td  bgcolor="#FFFFFF">操作</td>

</tr>

<?php

$total=0;

$array=explode("@",$_SESSION['producelist']);

$arrayquatity=explode("@",$_SESSION['quatity']);

while(list($name,$value)=each($_POST)) {

for($i=0;$i<count($array)-1;$i++) {

if(($array[$i])==$name) {

$arrayquatity[$i]=$value;

}

}

} //实现更改商品数量功能

$_SESSION['quatity']=implode("@", $arrayquatity);

for($i=0;$i<count($array)-1;$i++) {

$id=$array[$i];

$num=$arrayquatity[$i];

if($id!="") {

$sql=mysql_query("select * from tb_shangpin

where id='".$id."'");

$info=mysql_fetch_array($sql);

$total1=$num*$info['huiyuanjia'];

$total+=$total1;

$_SESSION["total"]=$total;

?>//计算商品总价

<tr>

<td height="25" bgcolor="#FFFFFF"><?php echo $info['mingcheng'];?></td>

<input type="text" name="<?php echo $info['id'];?>" size="2"  value=<?php echo $num;?>>

</td>

<td height="25" bgcolor="#FFFFFF">

<?php echo $info['shichangjia'];?>元</td>

<td height="25" bgcolor="#FFFFFF"><?php echo

$info['huiyuanjia'];?>元</td>

<td height="25" bgcolor="#FFFFFF"><?php echo

@(ceil(($info['huiyuanjia']/$info['shichangjia'])*100))."%";?></td>

<td height="25" bgcolor="#FFFFFF"><?php echo

$info['huiyuanjia']*$num."元";?></td>

<td height="25" bgcolor="#FFFFFF"><a href="#">

删除</a></td>

</tr> <?php }

} ?>

<tr>

<td height="25" colspan="6" bgcolor="#FFFFFF">

<table width="500" height="25">

<tr> <td width="125">

<input name="submit2" type="submit"  value=

"更改商品数量">

</td>

<td width="125"><a href="gouwusuan1.php?

dingdanhao="">去收银台</a>&nbsp;&nbsp;&nbsp;

<a href="#">返回</a></td>

<td width="125"><a href="gouwuche1.php?qingkong=

yes">清空购物车</a></td>

<td width="125">总计:<?php echo $total;?></td>

</tr>  <?php

}  ?>

</table> </form>

運行效果如图2所示:

4 结束语

随着越来越多的人选择网上购物,网上购物系统变得越来越重要[3]。利用PHP+MySQL开发的网上购物系统,具有商品信息浏览、商品搜索、购物车等多项功能,较好地满足了网上购物的需要。在整个开发过程中,采用了PHP技术、DW的框架技术等,界面清晰、美观,功能完备,具有一定的创新性。系统自运行以来,受到了用户的普遍欢迎。但由于时间紧迫,系统在页面布局等方面还有一定的欠缺,接下来要继续修改和完善该系统。

参考文献(References):

[1] 杨宇.PHP典型模块与项目实战大全[M].清华大学出版社,2012.

[2] 朱珍.PHP网站开发技术[M].电子工业出版社,2014.

[3] 沈蕴梅.电子商务网站建设—PHP+MySQL项目开发教程[M].北京理工大学出版社,2018.

[4] 孙号夕,胡伟,杨中宇.基于PHP和Mysql的销售管理系统的设计与实现[J].电脑知识与技术,2014.

[5] 徐辉,高荣.“jQuery Web前端开发应用技术”课程教学实践探索[J].福建电脑,2017.

猜你喜欢

购物车
被疫情改变的购物车
很萌!熊孩子清空7万元购物车
推购物车购物
购物车
跟我走
购物车里的“时间线”
清空你的购物车是我的温柔
清空购物车了吗!