• php大作业:普通用户浏览界面+购物车界面(为方便队友重组代码)


    1、代码部分说明

    putongmain.php(普通用户浏览商品主界面)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>商品界面</title>
        <style type="text/css">
            html{font-size:12px;}
            fieldset{width:850px; margin: 0 auto;}
            legend{font-weight:bold; font-size:14px;}
            label{float:left; 70px; margin-left:10px;}
            .left{margin-left:80px;}
            .input{150px;}
            .sousu{margin-left:210px; 300px}
            .sousubutton{margin-left:20px;}
        </style>
    </head>
    <body>
    <fieldset>
            <legend><font color="blue">商品浏览</font></legend>
            <form  method="post" action="save.php">
            <p>
                <input id="sousu" name="bname" type="text"  class="sousu" />
                <input type="submit" name="sousubutton" value="搜索"  class="sousubutton">
                <a href="gouwuche.php" target="_self" class="sousubutton">查看购物车</a>
            </p>
            </form>
    
            <table border="0px"cellspacing="20px" cellpadding="5px">
                <?php
                $conn = mysqli_connect("localhost", "root", "123", "db") or die("连接数据库服务器失败!".mysqli_error()); //连接MySQL服务器,选择数据库
                mysqli_query($conn,"set names utf8");                        //设置数据库编码格式utf8
                mysqli_select_db($conn,'db'); //选择数据库
                $q = "select * from bookinfo"; //SQL查询语句
                $rs=mysqli_query($conn,$q);
                //$rs = mysqli_query($q, $link); //获取数据集
                while($row = mysqli_fetch_row($rs)){
                    echo "<tr><td><img src='$row[3]'/></td><td>$row[1]</td><td>$row[2]元</td><td>$row[4]</td><td><a href='gouwuchuli.php?bid={$row[0]}'>加入购物车</a></td></tr>"; //显示数据
                }
                ?>
            </table>
        </fieldset>
    </body>
    </html>

    save.php

    <?php
    /**
     * Created by PhpStorm.
     * User: 米羊
     * Date: 2020/5/28
     * Time: 20:26
     */
    $bname=$_POST['bname'];
    header("location:sousu.php?bname=$bname ");

    sousu.php

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>商品界面</title>
        <style type="text/css">
            html{font-size:12px;}
            fieldset{width:850px; margin: 0 auto;}
            legend{font-weight:bold; font-size:14px;}
            label{float:left; 70px; margin-left:10px;}
            .left{margin-left:80px;}
            .input{150px;}
            .sousu{margin-left:210px; 300px}
            .sousubutton{margin-left:20px;}
        </style>
    </head>
    <body>
    <?php
    $bname=$_GET['bname'];
    ?>
    <fieldset>
        <legend><font color="blue">商品搜索</font></legend>
        <p>
            <input id="sousu" name="sousu" type="text"  class="sousu"  value="<?php echo $bname ?>"/>
            <input type="submit" name="sousubutton" value="搜索"  class="sousubutton">
            <a href="gouwuche.php" target="_self" class="sousubutton">查看购物车</a>
        <p/>
    <table border="0px"cellspacing="20px" cellpadding="5px">
        <?php
        $bname=$_GET['bname'];
        $conn = mysqli_connect("localhost", "root", "123", "db") or die("连接数据库服务器失败!".mysqli_error()); //连接MySQL服务器,选择数据库
        mysqli_query($conn,"set names utf8");                        //设置数据库编码格式utf8
        mysqli_select_db($conn,'db'); //选择数据库
        $q = "select * from bookinfo where bname like '%{$bname}%'"; //SQL查询语句
        $rs=mysqli_query($conn,$q);
        //$rs = mysqli_query($q, $link); //获取数据集
        $row = mysqli_fetch_array($rs);
        echo "<tr><td><img src='$row[3]'/></td><td>$row[1]</td><td>$row[2]</td><td>$row[4]</td><td><a href='gouwuchuli.php?ids={$row[1]}'>加入购物车</a></td></tr>"; //显示数据
        ?>
    </table>
    </fieldset>
    </body>
    </html>

     gouwuchuli.php(用户在浏览商品界面点击加入购物车后进行处理的代码)

    <?php
    /**
     * Created by PhpStorm.
     * User: 米羊
     * Date: 2020/5/30
     * Time: 10:11
     */
    $bid=$_GET['bid'];
    $conn = mysqli_connect("localhost", "root", "123", "db") or die("连接数据库服务器失败!".mysqli_error()); //连接MySQL服务器,选择数据库
    mysqli_query($conn,"set names utf8");                        //设置数据库编码格式utf8
    mysqli_select_db($conn,'db'); //选择数据库
    $q = "select * from gouwuche where bid='$bid'"; //SQL查询语句
    $rows=mysqli_query($conn,$q);
    if (mysqli_num_rows($rows) < 1){
       $sql="select * from bookinfo where bid='$bid'";
        $rs=mysqli_query($conn,$sql);
        $arr = mysqli_fetch_array($rs);
        $bid=$arr[0];
        $bname=$arr[1];
        $prince=$arr[2];
        $image=$arr[3];
        $num=1;
        $sql1="insert into gouwuche(bid,bname,prince,image,num) values('$bid','$bname',$prince,'$image',$num)";
        $rs1=mysqli_query($conn,$sql1)or die("插入失败".$sql1);
    }else{
        $arr1 = mysqli_fetch_array($rows);
        $bid=$arr1[0];
        $num=$arr1[4];
        $num=$num+1;
        $sql2="update gouwuche set num='$num' where bid='$bid'";
        $rs2=mysqli_query($conn,$sql2)or die("修改失败".$sql2);
    }
    header("location:putongmain.php");

    gouwuche.php(用来展示购物车)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>购物车界面</title>
        <style type="text/css">
            html{font-size:12px;}
            fieldset{width:900px; margin: 0 auto;}
            legend{font-weight:bold; font-size:14px;}
            label{float:left; 70px; margin-left:10px;}
            .left{
                margin-left:180px;
                font-size:14px
            }
            .input{150px;}
            .dingdan{margin-left:410px;}
            .fanhui{margin-left:50px;}
        </style>
    </head>
    <body>
    <?php
    $sum=0;
    $numsum=0;
    $conn = mysqli_connect("localhost", "root", "123", "db") or die("连接数据库服务器失败!".mysqli_error()); //连接MySQL服务器,选择数据库
    mysqli_query($conn,"set names utf8");                        //设置数据库编码格式utf8
    mysqli_select_db($conn,'db'); //选择数据库
    $q1 = "select * from gouwuche"; //SQL查询语句
    $rs1=mysqli_query($conn,$q1);
    //$rs = mysqli_query($q, $link); //获取数据集
    while($row1 = mysqli_fetch_row($rs1)){
        $prince=$row1[2];
        $num=$row1[4];
        $numsum=$numsum+$num;
        $sum=$sum+$num*$prince;
    }
    ?>
    <fieldset>
        <legend><font color="blue">购物车</font></legend>
            <p>
                <a href="putongmain.php" target="_self" class="fanhui">返回</a>
                <a href="adddingdan.php" target="_self" class="dingdan">提交订单</a>
            </p>
        <p class="left"><font color="blue">购物车中共有<?php echo $numsum  ?>件商品,总价<?php echo $sum  ?>元</font></p>
        <table border="0px"cellspacing="20px" cellpadding="5px">
            <tr>
                <th>书图片</th>
                <th>书名</th>
                <th>价格</th>
                <th>已购买数量</th>
                <th>购买</th>
                <th>取消购买</th>
            </tr>
            <?php
            $conn = mysqli_connect("localhost", "root", "123", "db") or die("连接数据库服务器失败!".mysqli_error()); //连接MySQL服务器,选择数据库
            mysqli_query($conn,"set names utf8");                        //设置数据库编码格式utf8
            mysqli_select_db($conn,'db'); //选择数据库
            $q = "select * from gouwuche"; //SQL查询语句
            $rs=mysqli_query($conn,$q);
            //$rs = mysqli_query($q, $link); //获取数据集
            while($row = mysqli_fetch_row($rs)){
                echo "<tr><td><img src='$row[3]'/></td><td>$row[1]</td><td>$row[2]元</td><td>$row[4]</td><td><a href='gouwuchuli1.php?bid={$row[0]}'>加入购物车</a></td><td><a href='quxiao.php?bid={$row[0]}'>取消购买</a></td></tr>"; //显示数据
            }
            ?>
        </table>
    </fieldset>
    </body>
    </html>

    gouwuchuli1.php(用户在购物车界面点击加入购物车后进行处理的代码)

    <?php
    /**
     * Created by PhpStorm.
     * User: 米羊
     * Date: 2020/5/30
     * Time: 10:11
     */
    $bid=$_GET['bid'];
    $conn = mysqli_connect("localhost", "root", "123", "db") or die("连接数据库服务器失败!".mysqli_error()); //连接MySQL服务器,选择数据库
    mysqli_query($conn,"set names utf8");                        //设置数据库编码格式utf8
    mysqli_select_db($conn,'db'); //选择数据库
    $q = "select * from gouwuche where bid='$bid'"; //SQL查询语句
    $rows=mysqli_query($conn,$q);
    if (mysqli_num_rows($rows) < 1){
        $sql="select * from bookinfo where bid='$bid'";
        $rs=mysqli_query($conn,$sql);
        $arr = mysqli_fetch_array($rs);
        $bid=$arr[0];
        $bname=$arr[1];
        $prince=$arr[2];
        $image=$arr[3];
        $num=1;
        $sql1="insert into gouwuche(bid,bname,prince,image,num) values('$bid','$bname',$prince,'$image',$num)";
        $rs1=mysqli_query($conn,$sql1)or die("插入失败".$sql1);
    }else{
        $arr1 = mysqli_fetch_array($rows);
        $bid=$arr1[0];
        $num=$arr1[4];
        $num=$num+1;
        $sql2="update gouwuche set num='$num' where bid='$bid'";
        $rs2=mysqli_query($conn,$sql2)or die("修改失败".$sql2);
    }
    header("location:gouwuche.php");

    quxiao.php(用户在购物车界面点击取消购买后进行处理的代码)

    <?php
    /**
     * Created by PhpStorm.
     * User: 米羊
     * Date: 2020/5/30
     * Time: 11:14
     */
    $bid=$_GET['bid'];
    $conn = mysqli_connect("localhost", "root", "123", "db") or die("连接数据库服务器失败!".mysqli_error()); //连接MySQL服务器,选择数据库
    mysqli_query($conn,"set names utf8");                        //设置数据库编码格式utf8
    mysqli_select_db($conn,'db'); //选择数据库
    $q = "select * from gouwuche where bid='$bid'"; //SQL查询语句
    $rs=mysqli_query($conn,$q);
    $arr = mysqli_fetch_array($rs);
    $num=$arr[4];
    if($num>1){
        $num=$num-1;
        $sql1="update gouwuche set num='$num' where bid='$bid'";
        $rs1=mysqli_query($conn,$sql1)or die("修改失败".$sql1);
    }else{
        $sql2="delete from gouwuche where bid='$bid'";
        $rs2=mysqli_query($conn,$sql2)or die("删除失败".$sql2);
    }
    header("location:gouwuche.php");

    image文件下放置的是书的图片

    2、数据库和表展示

    bookinfo表展示

     

     gouwuche表展示

     

     

     数据库名db,数据表名bookinfo

      数据库名db,数据表名bookinfo

     sql文件已经发到宿舍群里了

    3、实现效果图

    输如参数,点击搜索

     点击加入购物车

     

     在购物车界面,点击加入购物车,数量加1,取消购买,数量减1,若数量为0,则不显示此商品。

  • 相关阅读:
    什么是浮动IP
    How can I detect multiple logins into a Django web application from different locations?
    git add -A使用说明
    理解水平扩展和垂直扩展
    php != 和 !== 的区别
    wireshark:Couldn't run /usr/bin/dumpcap in child process: Permission denied
    Unable to VNC onto Centos server remotely
    systemctl使用说明
    KiB、MiB与KB、MB的区别
    python带setup.py的包的安装
  • 原文地址:https://www.cnblogs.com/yang2000/p/12983463.html
Copyright © 2020-2023  润新知