PHP下的购物车
导航 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Session购物车练习</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="" rel="stylesheet"> </head> <body> <h2>商品信息管理-购物车练习</h2> <a href="adds.php">添加商品</a> <a href="index.php">浏览商品</a> <a href="myshop.php">我的购物车</a> <a href="clear.php">清空购物车</a> <hr width="80%"> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Examples</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="" rel="stylesheet"> </head> <body> <form method="post" action="addcas.php" entype="multipart/form-data" name="aax"> <table border="0" width="300"> <tr> <td>名称</td> <td><input type="text" name=name /></td> </tr> <tr> <td>类型</td> <td> <select name="s"> <option value="1">服装</option> <option value="2">视频</option> <option value="3">汽车</option> </select> </td> </tr> <tr> <td align="right">单价:</td> <td><input type="text" name="price"/></td> </tr> <tr> <td align="right">库存:</td> <td><input type="text" name="total"/></td> </tr> <tr> <td align="right">图片:</td> <td><input type="file" name="pic"/></td> </tr> <tr> <td align="right" valign="top">描述:</td> <td><textarea rows="5" cols="20" name="note"></textarea></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" onclick="ajax();" value="添加"/> <input type="reset" value="重置"/> </td> </tr> </table> <div id="sed">x</div> </form> <script> function ajax() { var f=document.aax; var name=f.name.value; var typeid=f.s.value; var pic=f.pic.value; var price=f.price.value; var note=f.note.value; var xmlhttp=null; if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }else{ xmlhttp=ActiveXObject("Microsoft.XMLHTTP"); } var lian="name="+name+"&typeid="+typeid+"&pic="+pic+"&price="+price+"¬e="+note; $xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); xmlhttp.open("get","addcas.php",true); xmlhttp.send(null); xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("sed").innerHTML=$xmlhttp.responeseText; } }; } </script> </body> </html>
<?php $mysqli=new mysqli("127.0.0.1","root","","ce"); $name=$_POST['name']; $typeid=$_POST['s']; $pic=$_POST['pic']; $price=$_POST['price']; $total=$_POST['total']; $note=$_POST['note']; $addtime=time(); $sql="insert into goods(name,typeid,price,pic,note,addtime,total)values('{$name}','{$typeid}','{$price}','{pic}','{$note}','{$addtime}','{total}')"; $query=$mysqli->query($sql); if (!empty($query)) { echo "成功1<script>location.href='index.php'</script>"; }else{ echo "失败2<script>location.href='index.php'</script>"; } ?>
index.php <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Examples</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="" rel="stylesheet"> </head> <body> <?php include('menu.php'); ?> <table border="1"> <tr> <th>商品编号</th> <th>商品名称</th> <th>商品图片</th> <th>单价</th> <th>库存量</th> <th>添加时间</th> <th>操作</th> </tr> <?php $conn=new mysqli("127.0.0.1","root","","ce"); $sql="select * from goods"; $result=$conn->query($sql); //解析商品(结果集) while ($row=$result->fetch_assoc()) { echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['name']}</td>"; echo "<td><img src='./uploads/s_{$row['pic']} />'</td>"; echo "<td>{$row['price']}</td>"; echo "<td>{$row['total']}</td>"; echo "<td>".date('Y-m-d H:i:s',$row['addtime'])."</td>"; echo "<td><a href='action?action=del&id={$row['id']}&picname={$row['pic']}'>删除</a> <a href='edit.php?id={$row['id']}'>修改</a> <a href='add.php?id={$row['id']}'>添加购物</a> "; echo "</tr>"; } ?> </table> </body> </html>
myshop.php <?php session_start(); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Examples</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="" rel="stylesheet"> </head> <body> <table border="1" width="300"> <tr> <th>商品ID号</th> <th>商品名称</th> <th>商品图片</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> <?php $sum=0; //定义总金额 // echo $num; // var_dump($_SESSION['shops']); if(isset($_SESSION['shops'])) { foreach($_SESSION['shops'] as $v) { echo "<tr>"; echo "<th>{$v['id']}</th>"; echo "<th>{$v['name']}</th>"; echo "<th>{$v['pic']}</th>"; echo "<th>{$v['price']}</th>"; echo "<th>{$v['num']}</th>"; echo "<th>".($v['price']*$v['num'])."</th>"; echo "<th><a href='del.php?id={$v['id']}'>删除</a>"; echo "</tr>"; $sum+=$v['price']*$v['num']; } } ?> </table> </body> </html> <?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/5/25 * Time: 9:31 */
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/5/25
* Time: 12:53
*/
session_start();
if($_GET['id']){
unset($_SESSION['shops'][$_GET['id']]);
}else{
unset($_SESSION['shops']);
}