<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<p><a href="first.php">首页</a>&nbsp;&nbsp; <a href="cart_show.php">购物篮</a> &nbsp;&nbsp;<a href="history.php">历史记录</a> &nbsp;&nbsp;<a href="index.php">退出</a>
  
  <?php 
$con = mysql_connect("localhost", "root", "");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }$db_selected = mysql_select_db("user",$con);
$sql="select * from produce";//查询所有商品 
$rs=mysql_query($sql,$con);//执行sql语句,得到一个结果集 
while($row=mysql_fetch_array($rs))//遍历结果集 

?> 
</p>
<p>&nbsp;</p>
<table width="367" height="247" border="1" style="float:left"> 
  <tr> 
    <td width="127" rowspan="3"><img 
src="images/<?php echo $row["pimg"]?>" width="123" height="121" border="0" /></td> 
    <td width="224" height="35">货物名称:<?php echo $row["pname"] ?></td> 
  </tr> 
  <tr> 
    <td height="28">货物价格:<?php echo $row["price"] ?></td> 
  </tr> 
  <tr> 
    <td height="27" align="center"><a href="buy.php?id=<?php echo $row["pid"] ?>&pname=<?php 
echo $row["pname"] ?>">购买</a></td> 
  </tr>  </table> 
<?php 

?> 
</body>
</html>

解决方案 »

  1.   

    <?php
    $act=$_GET[act];
    $product_id=$_GET[prcid];if($act=="add")

    $cars_id=explode(",",$_COOKIE[buypro]);
    //echo "<pre>";
        //print_r($cars_id);
    //echo"</pre>";
    //exit;
    foreach($cars_id as $pr_id)
    {
     if($pr_id==$product_id)
     {
     echo "<script>alert('您已经购买了此商品,请查看购物车!');location.href='car.php';</script>";
     exit;
     }
       } if(empty($_COOKIE[buypro]))
    {
    setcookie("buypro",$product_id,time()+3600);
    setcookie("buycount","1",time()+3600);
    }
    else
    {
    $newpro=$_COOKIE[buypro].",".$product_id;
    $newcount=$_COOKIE[buycount].","."1";
    setcookie("buypro",$newpro,time()+3600);
    setcookie("buycount",$newcount,time()+3600);
    }
    }
    //删除
    if($act=="del")
    {
    $buy=$_COOKIE[buypro];
    $cars_id=explode(",",$buy);
    $buycountary=explode(",",$_COOKIE[buycount]);
    $newid=array();
    $carcount=array();
    for($i=0;$i<count($cars_id);$i++)
    {
     if($cars_id[$i]!=$product_id)
     {
     $newid[]=$cars_id[$i];
     $carcount[]=$buycountary[$i];
     }
       }
    $newpro=implode(",",$newid);
    $newcount=implode(",",$carcount);
    setcookie("buypro",$newpro,time()+3600);
    setcookie("buycount",$newcount,time()+3600);
    }//修改
    if($act=="up")
    {
    $car_count=$_POST[buycount];
    $temp=implode(",",$car_count);
    setcookie("buycount",$temp,time()+3600);
    }//全站清空
    if($act=="quan")
    {
    setcookie("buypro","",time()+3600);
    }
    echo "<script>location.href='car.php';</script>";
    ?>