使用mysql数据库和php技术实现的购物车效果

一、功能需求

(1)购物车商品数量可以随意增减;
(2)所有商品的总价和单个商品的总价随着商品数量变化而变化;
(3)可以删除商品;
(4)有清空购物车功能;

shopping.php页面效果

使用mysql数据库和php技术实现的购物车效果_第1张图片

list.php页面效果

使用mysql数据库和php技术实现的购物车效果_第2张图片

//shopping.php
<?php
$dsn="mysql:dbname=mysql;host=127.0.0.1";
$name="root";
$pwd="root";
$pdo=new PDO($dsn,$name,$pwd);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT);
$sql="select * from shanping";
$sth=$pdo->prepare($sql);
$sth->execute();
$res=$sth->fetchAll(PDO::FETCH_ASSOC);
if(isset($_POST["submit"])){
    try{
        $name=$_POST["name"];
        $price=$_POST["price"];
        $sql1="select * from shanping_copy1 where name=:name";
        $sth1=$pdo->prepare($sql1);
        $sth1->bindParam(":name",$name);
        $sth1->execute();
        $res1=$sth1->fetch(PDO::FETCH_ASSOC);
        $row_count =$sth1->rowCount();
        if($row_count){
            //检测是否已经加入购物车,如果购物车内已有该物品则直接加个数
            $sql1="UPDATE shanping_copy1 SET count = count+1 WHERE name = '$name'";
            $sth=$pdo->exec($sql1);
        }else{
            $sql1="insert into shanping_copy1(price,count,name) VALUES ('$price',1,'$name')";
            $sth1=$pdo->exec($sql1);
        }
    }catch(PDOException $e){
        echo $e->getMessage();
    }
}
if(isset($_POST["delete"])){

        $name=$_POST["name"];
        $sth1=$pdo->exec("DELETE FROM shanping WHERE name = '$name'");
        $sth2=$pdo->exec("DELETE FROM shanping_copy1 WHERE name = '$name'");
        header("Refresh:0.1;url=shopping.php");


}
echo "";echo"
"
;echo"";foreach($resas$value){echo"";}echo"";echo"
"; echo ""; echo "

$value[name] $value[price]

"
; echo "
"
; echo "
查看购物车
"
;
//list.php
<?php
$dsn="mysql:dbname=mysql;host=127.0.0.1";
$name="root";
$pwd="root";
$pdo=new PDO($dsn,$name,$pwd);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT);
$sql="select * from shanping_copy1";
$sth=$pdo->prepare($sql);
$sth->execute();
$res=$sth->fetchAll(PDO::FETCH_ASSOC);
$sum=0;
if(isset($_GET['all'])){
    $pdo->exec("Delete from shanping_copy1");
    header("Refresh:0.1;url=list.php");
}
if(isset($_GET['add'])){
    $c=$_GET['is_count'];
    $n=$_GET['is_name'];
        $pdo->exec("Update shanping_copy1 set count=$c+1 where name='$n'");
        header("Refresh:0.1;url=list.php");
}
if(isset($_GET['del'])){
    $c=$_GET['is_count'];
    $n=$_GET['is_name'];
    if($c<=0){
        $pdo->exec("Delete from shanping_copy1 where name='$n'");
        header("Refresh:0.1;url=list.php");
    }else{
        $pdo->exec("Update shanping_copy1 set count=$c-1 where name='$n'");
        header("Refresh:0.1;url=list.php");
    }

}
?>
<style>
  td{
      width: 100px;
      border: 2px solid;
  }
</style>
<form id="form_id" method="post" action="list.php">
    <table>
        <tr>
            <td align="center">商品名称</td>
            <td align="center">商品价格</td>
            <td align="center">数量</td>
            <td align="center">总金额</td>
            <td align="center">修改</td>
        </tr>
        <?php for($i=0;$i<count($res);$i++){
            ?>
            <tr>
            <td align="center"><input type="hidden" name="c_name[]" value="$res[$i]["name"]?>"><?php echo $res[$i]["name"]?></td>
            <td align="center"><?php echo $res[$i]["price"]?></td>
            <td align="center"><?php echo $res[$i]["count"]?></td>
            <td align="center"><?php echo $res[$i]["count"]*$res[$i]["price"]?></td>
            <td  align="center">
                <a href="list.php?add=add&is_name=$res[$i]['name']?>&is_count=$res[$i]['count']?>">
                    <img style="width: 15px;height: 15px;float: left;margin-top: 3px" src="images/add.png">
                </a>
                <input id = "count" style="border: 0;text-align: center;width: 50px" value="$res[$i]["count"];?>">

                <a href="list.php?del=del&is_name=$res[$i]["name"]?>&is_count=$res[$i]["count"];?>">
                    <img style="width: 15px;height: 15px;float: right;margin-top: 3px" src="images/del.png">
                </a>
            </td>
            <?php $sum=$sum+$res[$i]["count"]*$res[$i]["price"]?>
            </tr>

            </tr>

        <?php }?>
        <tr>
            <td colspan="5">商品总价格:<span><?php echo $sum?></span></td>
        </tr>
    </table>
</form>
<a href='list.php?all=alldel'>清空购物车</a><br>
<a href='shopping.php'>返回商品列表</a>


二、相关的sql数据库文件、shopping.php、list.php文件以及图片

上述相关文件及图片已上传至资源,需要的可自行下载
点击下载
使用mysql数据库和php技术实现的购物车效果_第3张图片

你可能感兴趣的:(php,前端开发,php,数据库,mysql)