120 likes | 245 Views
購 物 車. 1. 先 Create 購物車的 table 購物車的主表 shopping shopping( shoppingid char(40), not null, PK shoppingtime datetime ID char(10) ) 購物車的副表 shopping_detail shopping_detail ( sid int, autoincrement shoppingid char(40) not null,
E N D
購 物 車 1.先Create購物車的table 購物車的主表 shopping shopping( shoppingid char(40), not null, PK shoppingtime datetime ID char(10) ) 購物車的副表 shopping_detail shopping_detail ( sid int, autoincrement shoppingid char(40) not null, Pid char(20), amount int, )
修改檔案 Myhome.php 加入登出, 後台管理, 查看購物車選項 0315.Php 登入成功後回到myhome.php 加入登入者SESSION[“loginid”] Logout.php 清空登入者SESSION[“loginid”] Item.htm 多[檢視購物](view.php)選項
修改 myhome.php <?if($_SESSION['ok']==0){?> <form method="post" action="0315.php" name="lgin"> <table border="1" width="100%" id="table1"> <tr> <td align="right">身分證:</td> <td><input type="text" name="T1" size="20"></td> </tr> <tr> <td align="right">密碼:</td> <td><input type="password" name="T2" size="20"></td> </tr> <tr> <td align="right"><input type="submit" value="確定" name="B1"></td> <td><input type="reset" value="重新設定" name="B2"></td> </tr> </table> <p><a href=viewcar.php>查看購物車</a></p> </form> <?}else{?> <table border="1" width="100%" id="table1"> <tr> <td align="right"><a href=logout.php>登出</a></td> </tr> <tr> <td align="right"><a href=db.php>後台管理</a></td> </tr> <tr> <td align="right"><a href=viewcar.php>查看購物車</a></td> </tr> </table> <?}?>
<? //連線到資料庫 include("conn.php"); if (empty($_SESSION['buycarid'])){ //產生購物單編號 mt_srand((double)microtime()*1000000); $shpid=crypt(uniqid(mt_rand())); $_SESSION['buycarid']=$shpid; } if(empty($_SESSION["shoppinglist"])){ $_SESSION["shoppinglist"]=""; } $_SESSION["shoppinglist"]=$_SESSION["shoppinglist"].$_GET["buyid"].";"; //顯示結果 echo "您將訂購編號:".$_GET["buyid"]." 的產品。<p>"; echo "目前訂購:"; ?> <table border=1> <tr><td>產品編號</td><td>產品名稱</td></tr> <? $buyitem = strtok($_SESSION["shoppinglist"],";"); while($buyitem) { $str="select * from product where Pid='".$buyitem."'"; $result=mysql_query($str); if($row=mysql_fetch_array($result)){ ?> <tr> <td><?=$row["Pid"]?></td> <td><?=$row["Pname"]?></td> </tr> <? } $buyitem = strtok(";"); } ?> </table> <? echo "[<a href=myhome.php>繼續訂購</a> [<a href=pay.php>結帳</a>]";?> 購物車 putin.php 利用session產生購物編號
檢視購物車 viewcar.php <? //連線到資料庫 include("conn.php"); if (empty($_SESSION["shoppinglist"])){ echo "購物車是空的"; header("Refresh:2;url=myhome.php"); exit; } ?> <table border=1> <tr><td>產品編號</td><td>產品名稱</td></tr> <? $buyitem = strtok($_SESSION["shoppinglist"],";"); while($buyitem) { $str="select * from product where Pid='".$buyitem."'"; $result=mysql_query($str); if($row=mysql_fetch_array($result)){ ?> <tr> <td><?=$row["Pid"]?></td> <td><?=$row["Pname"]?></td> </tr> <? } $buyitem = strtok(";"); } ?> </table> <? echo "[<a href=myhome.php>繼續訂購</a> [<a href=pay.php>結帳</a>] [<a href=clearcar.php>清空購物車</a>]"; ?>
購物車 clearcar.php 清空購物車 <? $_SESSION["shoppinglist"]=""; echo "已清空<br>"; echo "[<a href=myhome.php>繼續訂購</a>]"; ?>
結帳 pay.php <? if($_SESSION['ok']==0){ //未登入 echo "需要先登入才能結帳"; header("Refresh:5;url=myhome.php"); exit; } //連線到資料庫 include("conn.php"); if (empty($_SESSION['buycarid'])){ echo "沒有購買任何東西"; }else{ //寫入shopping $str="insert into shopping (shoppingid,shoppingtime,ID) values ('".$_SESSION['buycarid']."',now(),'".$_SESSION['loginid']."')"; mysql_query($str); } $buyitem = strtok($_SESSION["shoppinglist"],";"); while($buyitem) { $str="insert into shopping_detail (shoppingid,Pid) values ('".$_SESSION['buycarid']."','".$buyitem."')"; mysql_query($str); $buyitem = strtok(";"); } $_SESSION["shoppinglist"]=""; $_SESSION['buycarid']=""; echo "結帳完畢 [<a href=myhome.php>繼續訂購</a>";?>
<? include("iflogin.php"); include("conn.php"); include("item.htm"); //串連table $str="select shopping.shoppingid as shoppingid,shopping.shoppingtime as shoppingtime,member.Name as Name from shopping,member where shopping.ID=member.ID and member.ID='".$_SESSION['loginid']."' order by shopping.shoppingtime desc"; $result=mysql_query($str); ?> 以下是已訂購的單據:<p> <table border=1> <tr> <td>編號</td> <td>購買編號</td> <td>購買時間</td> <td>購買人</td> </tr> <? $serial=1; while ($row=mysql_fetch_array($result)){ ?> <tr> <td><?=$serial?></td> <td><a href=viewdetail.php?buyid=<?=$row["shoppingid"]?> target=_blank><?=$row["shoppingid"]?></a></td> <td><?=$row["shoppingtime"]?></td> <td><?=$row["Name"]?></td> </tr> <? $serial=$serial+1; } ?> </table> <? include("ctu.htm"); ?> 檢視結果 view.php
<? include("iflogin.php"); include("conn.php"); //串連table $str="select shopping_detail.Pid as Pid,shopping_detail.amount as amount,product.Pname as Pname,product.price as price from shopping_detail,product where shopping_detail.shoppingid='".$_GET["buyid"]."' and shopping_detail.Pid=product.Pid"; $result=mysql_query($str); ?> 以下是訂購的產品:<p> <table border=1> <tr> <td>編號</td> <td>產品編號</td> <td>產品名稱</td> <td>單價</td> <td>數量</td> </tr> <? $serial=1; $total=0; while ($row=mysql_fetch_array($result)){ ?> <tr> <td><?=$serial?></td> <td><?=$row["Pid"]?></td> <td><?=$row["Pname"]?></td> <td><?=$row["price"]?></td> <td><?=$row["amount"]?></td> </tr> <? $total=$total+$row["price"]*$row["amount"]; $serial=$serial+1; } ?> </table> 總金額<?=$total?> 購物細表 viewdetail.php
if(empty($_SESSION["shoppinglist"])){ $_SESSION["shoppinglist"]=""; } if($_GET["del"]==1){ //刪除 // 由$_SESSION["shoppinglist"]找出要刪除的資料 $_SESSION["shoppinglist"]=ereg_replace($_GET["delid"].";","",$_SESSION["shoppinglist"]); }else{ $_SESSION["shoppinglist"]=$_SESSION["shoppinglist"].$_GET["buyid"].";"; //顯示結果 echo "您將訂購編號:".$_GET["buyid"]." 的產品。<p>"; echo "目前訂購:"; } ?> <table border=1> <tr><td>產品編號</td><td>產品名稱</td><td>刪除</td></tr> <? $buyitem = strtok($_SESSION["shoppinglist"],";"); while($buyitem) { $str="select * from product where Pid='".$buyitem."'"; $result=mysql_query($str); if($row=mysql_fetch_array($result)){ ?> <tr> <td><?=$row["Pid"]?></td> <td><?=$row["Pname"]?></td> <td><a href=putin.php?del=1&delid=<?=$row["Pid"]?>>刪除</a></td> </tr> 修改 putin.php變成可以刪除
if (empty($_SESSION['buycarid']) or empty($_SESSION['shoppinglist'])){ echo "沒有購買任何東西<br>"; echo "結帳完畢 [<a href=myhome.php>繼續訂購</a>]"; exit; }else{ //寫入shopping $str="insert into shopping (shoppingid,shoppingtime,ID) values ('".$_SESSION['buycarid']."',now(),'".$_SESSION['loginid']."')"; mysql_query($str); } $buyitem = strtok($_SESSION["shoppinglist"],";"); while($buyitem) { $str="insert into shopping_detail (shoppingid,Pid) values ('".$_SESSION['buycarid']."','".$buyitem."')"; mysql_query($str); $buyitem = strtok(";"); } 修改 pay.php
$buyitem = strtok($_SESSION["shoppinglist"],";"); $content="您好\n 您於建國購物網購買了下列產品:\n"; while($buyitem) { $str1="select Pname from product where Pid='".$buyitem."'"; $result=mysql_query($str1); $row=mysql_fetch_array($result); $content=$content.$buyitem." ".$row["Pname"]."\n"; $str="insert into shopping_detail (shoppingid,Pid) values ('".$_SESSION['buycarid']."','".$buyitem."')"; mysql_query($str); $buyitem = strtok(";"); } $_SESSION["shoppinglist"]=""; $_SESSION['buycarid']=""; // 寄出mail給購買人 $poster="建國購物網"; $email="abc@aa.bb.cc"; $extra="from: \"" . $poster . "\" <" . $email . ">\nContent-Type: text/plain;charset=\"big5\"\n Content-Transfer-Encoding: 7bit\n"; mail("rcjao@ctu.edu.tw","購物",$content,$extra); 修改 pay.php結合mail功能