php--一个简单的在线商店
发布日期:2021-05-06 21:18:47 浏览次数:34 分类:原创文章

本文共 8463 字,大约阅读时间需要 28 分钟。

seestore.php


<?php/** * Created by PhpStorm. */$conn = mysqli_connect('localhost','root','','php_project01');$display_block = "<h1>商品目录</h1><p>选择目录浏览其含有的商品。</p>";$get_cats_sql = "select id,cat_title,cat_desc from store_categories order by cat_title";$get_cats_res = mysqli_query($conn,$get_cats_sql) or die(mysqli_error($conn));if(mysqli_num_rows($get_cats_res) < 1){    $display_block = "<p><strong>对不起,目录下均没有商品存在!</strong></p>";}else{    while ($info = mysqli_fetch_array($get_cats_res))    {        $cat_id = $info['id'];        $cat_title = strtoupper(stripslashes($info['cat_title']));        $cat_desc = stripslashes($info['cat_desc']);        $display_block .="<p><strong><a href='".$_SERVER['PHP_SELF']."?cat_id=$cat_id'>$cat_title</a></strong>        &lt;$cat_desc&gt;</p>";        //如果点击了某个目录,需要显示目录下的商品        if(isset($_GET['cat_id']) && ($_GET['cat_id'] == $cat_id))        {            $safe_cat_id = mysqli_real_escape_string($conn,$_GET['cat_id']); //防止sql注入            $get_items_sql = "select id,item_title,item_price from store_items where cat_id=$safe_cat_id order by item_price desc";            $get_items_res = mysqli_query($conn,$get_items_sql) or die(mysqli_error($conn));            if(mysqli_num_rows($get_cats_res) < 1)            {                $display_block = "<p><strong>对不起,此目录没有商品!</strong></p>";            }            else            {                $display_block .= "<ul>";                while($item = mysqli_fetch_array($get_items_res))                {                    $item_id = $item['id'];                    $item_title = stripslashes($item['item_title']);                    $item_price = $item['item_price'];                    $display_block .= "<li><a href='showitem.php?item_id=$item_id'>[$item_title]</a>(¥$item_price)</li>";                }                $display_block .="</ul>";            }            mysqli_free_result($get_items_res);        }    }}mysqli_free_result($get_cats_res);mysqli_close($conn);?><html><head>    <title>商品目录</title></head><body><?php echo $display_block;?></body></html>

showitem.php


<?php/** * Created by PhpStorm. */$conn = mysqli_connect('localhost','root','','php_project01');$display_block = "<h1>商店~商品明细</h1>";$item_id = mysqli_real_escape_string($conn,$_GET['item_id']);$get_item_sql = "select c.id as cat_id,c.cat_title,si.item_title,si.item_price,si.item_desc,si.item_image        from store_items as si left join store_categories as c on c.id=si.cat_id where si.id=$item_id";$get_item_res = mysqli_query($conn,$get_item_sql);if(mysqli_num_rows($get_item_res) < 1){    $display_block .= "<p><strong>商品不存在!</strong></p>";}else{    while($info = mysqli_fetch_array($get_item_res))    {        $cat_id = $info['cat_id'];        $cat_title = strtoupper(stripslashes($info['cat_title']));        $item_title = stripslashes($info['item_title']);        $item_price = $info['item_price'];        $item_desc = stripslashes($info['item_desc']);        $item_image = $info['item_image'];    }    $display_block .= <<< OF_TEXT    <p><strong>你在浏览</strong>--&gt;    <strong><a href="seestore.php?cat_id=$cat_id">$cat_title</a>&gt;$item_title</strong></p>    <div style="float: left;"><img src="images/$item_image"></div>    <div style="float: left;padding-left: 12px">    <p><strong>详细:</strong><br>$item_desc</p>    <p><strong>价格:</strong>¥$item_price</p>OF_TEXT;    mysqli_free_result($get_item_res);    $get_colors_sql = "select item_color from store_item_color where item_id=$item_id order by item_color";    $get_colors_res = mysqli_query($conn,$get_colors_sql);    if(mysqli_num_rows($get_colors_res) > 0)    {        $display_block .= "<p>可选颜色:<br>";        while($colors = mysqli_fetch_array($get_colors_res))        {            $item_color = $colors['item_color'];            $display_block .= $item_color."<br>";        }        $display_block .= "</p>";    }    mysqli_free_result($get_colors_res);    $get_size_sql = "select item_size from store_item_size where item_id=$item_id order by item_size desc";    $get_size_res  = mysqli_query($conn,$get_size_sql);    if(mysqli_num_rows($get_size_res) > 0)    {        $display_block .="<p><strong>可选尺码:</strong><br>";        while($sizes = mysqli_fetch_array($get_size_res))        {            $item_size = $sizes['item_size'];            $display_block .= $item_size."<br>";        }        $display_block .= "</p>";    }    mysqli_free_result($get_size_res);    $display_block .= "</div>";}?><html><head>    <title>商品明细</title></head><body><?php echo $display_block;?></body></html>

数据库文件


-- phpMyAdmin SQL Dump-- version 4.8.4-- https://www.phpmyadmin.net/---- 主机: 127.0.0.1-- 生成日期: 2019-06-02 -- 服务器版本: 10.1.37-MariaDB-- PHP 版本: 7.3.1SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";SET AUTOCOMMIT = 0;START TRANSACTION;SET time_zone = "+00:00";/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8mb4 */;---- 数据库: `php_project01`---- ------------------------------------------------------------ 表的结构 `store_categories`--CREATE TABLE `store_categories` (  `id` int(11) NOT NULL,  `cat_title` varchar(50) DEFAULT NULL,  `cat_desc` text) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 `store_categories`--INSERT INTO `store_categories` (`id`, `cat_title`, `cat_desc`) VALUES(1, '帽子', '弗兰克帽子有多种颜色和尺寸供您挑选'),(2, '衬衫', '海澜之家衬衫有多种颜色和尺码供您选择'),(3, '书籍', '当当网有很多门类的书籍供您购买');-- ------------------------------------------------------------ 表的结构 `store_items`--CREATE TABLE `store_items` (  `id` int(11) NOT NULL,  `cat_id` int(11) NOT NULL,  `item_title` varchar(75) DEFAULT NULL,  `item_price` float(8,2) DEFAULT NULL,  `item_desc` text,  `item_image` varchar(50) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 `store_items`--INSERT INTO `store_items` (`id`, `cat_id`, `item_title`, `item_price`, `item_desc`, `item_image`) VALUES(1, 1, '篮球帽', 12.00, '弗兰克牌,短帽檐篮球帽', 'hat_1.gif'),(2, 1, '牛仔帽', 52.00, '加仑牌,长帽檐牛仔帽', 'hat_1.gif'),(3, 1, '顶帽', 102.00, '顾客最喜欢的帽子', 'hat_1.gif'),(4, 2, '短袖T恤', 12.00, '百分之百纯棉', 'shirt_1.gif'),(5, 2, '长袖T恤', 15.00, '风格与短袖T恤一样,纯棉', 'shirt_1.gif'),(6, 2, '针织衫', 22.00, '厚款,冬季款', 'shirt_1.gif'),(7, 3, '简妮的自我救赎', 12.00, '给你一个人生的忠告', 'book_1.gif'),(8, 3, '经济学', 35.00, '学校的教科书,容易让人犯困', 'book_1.gif'),(9, 3, '芝加哥的风格', 9.99, '最优秀的作家编写的', 'book_1.gif');-- ------------------------------------------------------------ 表的结构 `store_item_color`--CREATE TABLE `store_item_color` (  `id` int(11) NOT NULL,  `item_id` int(11) NOT NULL,  `item_color` varchar(25) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 `store_item_color`--INSERT INTO `store_item_color` (`id`, `item_id`, `item_color`) VALUES(1, 1, '红'),(2, 1, '绿'),(3, 1, '蓝');-- ------------------------------------------------------------ 表的结构 `store_item_size`--CREATE TABLE `store_item_size` (  `id` int(11) NOT NULL,  `item_id` int(11) NOT NULL,  `item_size` varchar(25) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 `store_item_size`--INSERT INTO `store_item_size` (`id`, `item_id`, `item_size`) VALUES(1, 1, '均码'),(2, 2, '均码'),(3, 3, '均码'),(4, 4, 'S'),(5, 4, 'M'),(6, 4, 'L'),(7, 4, 'XL'),(8, 5, 'S'),(9, 5, 'M'),(10, 5, 'L'),(11, 5, 'XL'),(12, 6, 'S'),(13, 6, 'M'),(14, 6, 'L'),(15, 6, 'XL');---- 转储表的索引------ 表的索引 `store_categories`--ALTER TABLE `store_categories`  ADD PRIMARY KEY (`id`),  ADD UNIQUE KEY `cat_title` (`cat_title`);---- 表的索引 `store_items`--ALTER TABLE `store_items`  ADD PRIMARY KEY (`id`);---- 表的索引 `store_item_color`--ALTER TABLE `store_item_color`  ADD PRIMARY KEY (`id`);---- 表的索引 `store_item_size`--ALTER TABLE `store_item_size`  ADD PRIMARY KEY (`id`);---- 在导出的表使用AUTO_INCREMENT------ 使用表AUTO_INCREMENT `store_categories`--ALTER TABLE `store_categories`  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;---- 使用表AUTO_INCREMENT `store_items`--ALTER TABLE `store_items`  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;---- 使用表AUTO_INCREMENT `store_item_color`--ALTER TABLE `store_item_color`  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;---- 使用表AUTO_INCREMENT `store_item_size`--ALTER TABLE `store_item_size`  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=16;COMMIT;/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

 

上一篇:php--一个带购物车的在线商店
下一篇:php--如何编写一个简易的论坛

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月13日 07时45分26秒