1 <html>
2 <head>
3 <title>商品信息管理</title>
4 </head>
5 <body>
6 <center>
7 <?php
8 include("menu.php");//导入导航栏
9 //1.导入配置文件
10 require("dbconfig.php");
11 //2.连接数据库,并选择数据库
12 $link = @mysql_connect(HOST,USER,PASS) or die("数据库连接失败");
13 mysql_select_db(DBNAME,$link);
14 //3.获取要修改的商品信息
15 $sql="select *from goods where id={$_GET['id']}";
16 $result = mysql_query($sql,$link);
17 //4.判断是否获取到要编辑的商品信息
18 if($result&&mysql_num_rows($result)>0)
19 {
20 $shop=mysql_fetch_assoc($result);//解析出要修改的商品信息
21 }else
22 {
23 die("没有找到要修改的商品信息");
24 }
25
26 ?>
27 <h3>编辑商品信息</h3>
28 <form action="action.php?action=update" enctype="multipart/form-data" method="post">
29 <input type="hidden" name="id" value="<?php echo $shop['id']; ?>" />
30 <input type="hidden" name="oldpic" value="<?php echo $shop['pic']; ?>" />
31
32 <table border="0" width="300">
33 <tr>
34 <td align="right">名称:</td>
35 <td><input type="text" name="name" value="<?php echo $shop['name']; ?>" /></td>
36 </tr>
37 <tr>
38 <td align="right">类型:</td>
39 <td>
40 <select name="typeid">
41 <?php
42 include("dbconfig.php");
43 foreach($typelist as $k=>$v)
44 {
45 $sd = ($shop['typeid']==$k)?"selected":"";//判断是否当前的类型
46 echo "<option value='{$k}' {$sd}>{$v}</option>";
47 }
48 ?>
49 </select>
50 </td>
51 </tr>
52 <tr>
53 <td align="right">单价:</td>
54 <td><input type="text" name="price" value="<?php echo $shop['price']; ?>" /></td>
55 </tr>
56 <tr>
57 <td align="right">库存:</td>
58 <td><input type="text" name="total" value="<?php echo $shop['total']; ?>" /></td>
59 </tr>
60 <tr>
61 <td align="right">图片:</td>
62 <td><input type="file" name="pic"/></td>
63 </tr>
64 <tr>
65 <td align="right" valign="top">描述:</td>
66 <td><textarea rows="5" cols="20" name="note"><?php echo $shop['note']; ?></textarea></td>
67 </tr>
68
69 <tr>
70 <td colspan="2" align="center">
71 <input type="submit" value="修改" />
72 <input type="reset" value="重置" />
73 </td>
74 </tr>
75 <tr>
76 <td align="right" valign="top"> </td>
77 <td><img src="./uploads/<?php echo $shop['pic'];?>" /></td>
78 </tr>
79 </table>
80 </form>
81 </center>
82 </body>
83 </html>
1 <?php
2 //公共函数库
3
4 /*
5 * 文件上传处理函数
6 * @param string filename 要上传的文件表单项名
7 * @param string $path 上传文件的保存路径
8 * @param array 允许的文件类型
9 * @return array 两个单元: ["error"] false:失败,ture:成功
10 * ["info"] 存放失败原因或成功的文件名
11 */
12
13 function uploadFile($filename,$path,$typelist=null)
14 {
15 //1.获取上传文件的名字
16 $upfile = $_FILES[$filename];
17 if(empty($typelist))
18 {
19 $typelist=array("image/gif","image/jpg","image/jpeg","image/png","image/pjpeg","image/x-png");//允许的文件类型
20 }
21 $res=array("error"=>false);//存放返回的结果
22 //2.过滤上传文件的错误号
23 if($upfile["error"]>0)
24 {
25 switch($upfile["error"])
26 {
27 case 1:
28 $res["info"]="上传的文件超过了 php.ini中upload_max_filesize选项大小";
29 break;
30 case 2:
31 $res["info"]="上传文件的大小超过了HTML表单中MAX_FILE_SIZE选项";
32 break;
33 case 3:
34 $res["info"]="文件只有部分被上传";
35 break;
36 case 4:
37 $res["info"]="没有文件被上传";
38 break;
39 case 6:
40 $res["info"]="找不到临时文件夹";
41 break;
42 case 7:
43 $res["info"]="文件写入失败";
44 break;
45 default:
46 $res["info"]="未知错误!";
47 break;
48
49 }
50 return $res;
51 }
52 //3.本次文件大小的限制
53 if($upfile["size"]>1000000)
54 {
55 $res["info"]="上传文件过大!";
56 return $res;
57 }
58 //4.过滤类型
59 if(!in_array($upfile["type"],$typelist))
60 {
61 $res["info"]="上传类型不符!".$upfile["type"];
62 return $res;
63 }
64 //5.初始化下信息(为图片产生一个随机的名字)
65 $fileinfo = pathinfo($upfile["name"]);
66 do
67 {
68 $newfile = date("YmdHis").rand(1000,9999).".".$fileinfo["extension"];//随机产生名字
69
70 }while(file_exists($newfile));
71 //6.执行上传处理
72 if(is_uploaded_file($upfile["tmp_name"]))
73 {
74 if(move_uploaded_file($upfile["tmp_name"],$path."/".$newfile))
75 {
76 //将上传成功后的文件名赋给返回数组
77 $res["info"]=$newfile;
78 $res["error"]=true;
79 return $res;
80 }else
81 {
82 $res["info"]="上传文件失败!";
83 }
84 }else
85 {
86 $res["info"]="不是一个上传的文件";
87 }
88 return $res;
89 }
90 //==================================================
91 /*
92 *
93 * 等比缩放函数(以保存的方式实现)
94 * @param string $picname 被缩放的处理图片源
95 * @param int $maxx 缩放后的图片的最大宽度
96 * @param int $maxy 缩放后图片的最大高度
97 * @param string $pre 缩放后图片名的前缀名
98 * @param string 返回后的图片名称(带路径),如a.jpg=>s_a.jpg
99 */
100 function imageUpdateSize($picname,$maxx=100,$maxy=100,$pre="s_"){
101 $info=getimagesize($picname); //获取图片的基本信息
102 $w = $info[0];//获取宽度
103 $h = $info[1]; // 获取高度
104 switch($info[2]){
105 case 1: //gif
106 $im=imagecreatefromgif($picname);
107 break;
108 case 2: //jpg
109 $im=imagecreatefromjpeg($picname);
110 break;
111 case 3: //png
112 $im=imagecreatefrompng($picname);
113 break;
114 default :
115 die("图片类型错误");
116 }
117 //计算缩放比例
118 if(($maxx/$w)>($maxy/$h)){
119 $b=$maxy/$h;
120 }else{
121 $b=$maxx/$w;
122 }
123 //计算缩放后的尺寸
124 $nw=floor($w*$b);
125 $nh=floor($h*$b);
126 //创建一个新的图像源
127 $nim=imagecreatetruecolor($nw,$nh);
128 //执行等比缩放
129 imagecopyresampled($nim,$im,0,0,0,0,$nw,$nh,$w,$h);
130 //输出图像
131 $picinfo=pathinfo($picname);
132 $newpicname=$picinfo["dirname"]."/".$pre.$picinfo["basename"];
133
134 switch($info[2]){
135 case 1:
136 imagegif($nim,$newpicname);
137 break;
138 case 2:
139 imagejpeg($nim,$newpicname);
140 break;
141 case 3:
142 imagepng($nim,$newpicname);
143 break;
144 default:
145 echo "图片压缩错误";
146 }
147 //释放图片资源
148 imagedestroy($im);
149 imagedestroy($nim);
150 //返回结果
151 return $newpicname;
152 }
1 <h2>商品信息管理--购物车</h2>
2 <a href="index.php">浏览商品</a>|
3 <a href="add.php">添加商品</a>|
4
5 <a href="myCart.php">我的购物车</a>|
6 <a href="clearCart.php">清空购物车</a>
7
8
9 <hr width=80%/>
1 <?php
2 session_start();//启动会话
3
4 ?>
5 <html>
6 <head>
7 <title>商品信息管理</title>
8 </head>
9 <body>
10 <center>
11 <?php include("menu.php");//导入导航栏 ?>
12 <h3>添加商品到购物车<h3>
13
14 <?php
15 //从数据库中读取要购买的信息并添加到购物车中
16 //1.导入配置文件
17 require("dbconfig.php");
18 //2.连接数据库,并选择数据库
19 $link = @mysql_connect(HOST,USER,PASS) or die("数据库连接失败");
20 mysql_select_db(DBNAME,$link);
21 //3.执行商品信息查询(获取要购买的信息)
22 $sql="select * from goods where id={$_GET['id']}";
23 $result = mysql_query($sql,$link);
24
25 //4.判断是否没有找到要购买的信息,若有就读取出要购买的信息
26 if(empty($result) || mysql_num_rows($result)==0)
27 {
28 die("没有找到要购买的信息!");
29 }else
30 {
31 $shop = mysql_fetch_assoc($result);
32 }
33 $shop["num"]=1;//添加一个数量的字段
34 //5.放入购物车中(若已存在的商品实现数量累加)
35 if(isset($_SESSION["shoplist"]{$shop['id']}))
36 {
37 //若存在数量增加1
38 $_SESSION["shoplist"][$shop['id']]["num"]++;
39 }else
40 {
41 //若不存在,作为新购买的商品添加到购物车中
42 $_SESSION["shoplist"][$shop['id']]=$shop;
43 }
44
45 ?>
46 </table>
47 </center>
48 </body>
49 </html>
1 <?php
2 session_start();//启动会话
3
4 ?>
5 <html>
6 <head>
7 <title>商品信息管理</title>
8 </head>
9 <body>
10 <center>
11 <?php include("menu.php");//导入导航栏 ?>
12 <h3>浏览我的购物车<h3>
13 <table border="1" width="600">
14 <tr>
15 <th>商品id号</th>
16 <th>商品名称</th>
17 <th>商品图片</th>
18 <th>单价</th>
19 <th>数量</th>
20 <th>小计</th>
21 <th>操作</th>
22 </tr>
23 <?php
24 $sum =0;//定义总金额的变量
25 if(isset($_SESSION["shoplist"])){
26 foreach($_SESSION["shoplist"] as $v)
27 {
28 echo "<tr>";
29 echo "<td>{$v['id']}</td>";
30 echo "<td>{$v['name']}</td>";
31 echo "<td><img src='./uploads/s_{$v['pic']}' /></td>";
32 echo "<td>{$v['price']}</td>";
33 echo "<td align='center'>
34 <button onclick='window.location.href="updateCart.php?id={$v['id']}&num=-1"'>-</button>
35 {$v['num']}
36 <button onclick='window.location.href="updateCart.php?id={$v['id']}&num=+1"'>+</button>
37 </td>";
38 echo "<td>".($v["price"]*$v['num'])."</td>";
39 echo "<td><a href='clearCart.php?id={$v['id']}'>删除</a></td>";
40 echo "</tr>";
41 $sum+=$v["price"]*$v['num'];//累计金额
42 }
43 }
44 ?>
45 <tr>
46 <th>总计金额:</th>
47 <th colspan ="5" align="right"><?php echo $sum; ?></th>
48 <td> </td>
49 </tr>
50 </table>
51 </center>
52 </body>
53 </html>
1 <?php
2
3 //删除购物车session中的信息
4 session_start();//启动会话
5
6 //判断是删除一个商品还是清空购物车
7 if($_GET['id'])
8 {
9 //只删除一种商品
10 unset($_SESSION['shoplist'][$_GET['id']]);
11 }else
12 {
13 //清空session中的商品
14 unset($_SESSION["shoplist"]);
15 }
16
17
18 //跳转到浏览购物车界面
19 header("Location:myCart.php");
20 ?>
1 <?php
2 session_start();//启动会话
3 //修改购物车中的信息
4
5 //获取要修改的信息
6
7 $id = $_GET['id'];
8 $num = $_GET['num'];
9
10 //修改商品信息
11 $_SESSION["shoplist"][$id]["num"]+=$num;
12
13 //防止商品数量过小
14 if($_SESSION["shoplist"][$id]["num"]<1)
15 {
16 $_SESSION["shoplist"][$id]["num"]=1;
17 }
18 //跳转回我的购物车界面
19 header("Location:myCart.php");
20
21 ?>