🗣 Wd提问: 点餐系统源码PHP
🤖 Ai回答: 以下是一个简单的 PHP 点餐系统源码示例,包含基本功能:菜品展示、添加到购物车、下单等。该系统使用 MySQL 数据库存储数据,适合初学者学习和扩展。
📁 项目结构
point-order-system/
├── index.php 首页(菜单展示)
├── cart.php 购物车页面
├── order.php 下单页面
├── add_to_cart.php 添加商品到购物车
├── db.php 数据库连接
├── config.php 配置文件
└── database/
├── dishes.sql 初始化数据库脚本
✅ 1、`db.php` — 数据库连接
php
<?php
$host = 'localhost';
$dbname = 'restaurant';
$username = 'root';
$password = '';
try {
$pdo = new PDO(mysql:host=$host;dbname=$dbname, $username, $password);
$pdo-setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die(连接失败: 、$e-getMessage());
}
?
✅ 2、`dishes.sql` — 创建数据库表
sql
CREATE DATABASE IF NOT EXISTS restaurant;
USE restaurant;
CREATE TABLE IF NOT EXISTS dishes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10,2) NOT NULL,
description TEXT,
image VARCHAR(255)
);
INSERT INTO dishes (name, price, description, image) VALUES
('宫保鸡丁', 28.00, '经典川菜,麻辣鲜香', 'gongbao.jpg'),
('红烧肉', 32.00, '肥而不腻,入口即化', 'hongshao.jpg'),
('麻婆豆腐', 18.00, '麻辣嫩滑,下饭神器', 'mapo.jpg'),
('清炒时蔬', 15.00, '新鲜蔬菜,健康之选', 'chao.shu.jpg');
✅ 3、`index.php` — 菜单展示页
php
<?php
require_once 'db.php';
$stmt = $pdo-query(SELECT * FROM dishes);
$dishes = $stmt-fetchAll();
?
<!DOCTYPE html
<html lang=zh
<head
<meta charset=UTF-8
<title点餐系统</title
<style
body { font-family: Arial, sans-serif; margin: 20px; }
.dish { border: 1px solid ccc; padding: 10px; margin: 10px; display: inline-block; width: 250px; }
.dish img { width: 100%; height: 150px; object-fit: cover; }
.price { color: red; font-weight: bold; }
button { margin-top: 10px; padding: 5px 10px; }
</style
</head
<body
<h1欢迎光临餐厅</h1
<div
<?php foreach ($dishes as $dish): ?
<div class=dish
<img src=<?= htmlspecialchars($dish['image']) ? alt=<?= htmlspecialchars($dish['name']) ?
<h3<?= htmlspecialchars($dish['name']) ?</h3
<p class=price¥<?= number_format($dish['price'], 2) ?</p
<p<?= htmlspecialchars($dish['description']) ?</p
<form action=add_to_cart.php method=POST
<input type=hidden name=dish_id value=<?= $dish['id'] ?
<input type=number name=quantity min=1 value=1 required
<button type=submit加入购物车</button
</form
</div
<?php endforeach; ?
</div
<a href=cart.php查看购物车</a
</body
</html
✅ 4、`add_to_cart.php` — 添加商品到购物车
php
<?php
session_start();
require_once 'db.php';
if ($_POST['dish_id']) {
$dish_id = $_POST['dish_id'];
$quantity = $_POST['quantity'];
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = [];
}
if (isset($_SESSION['cart'][$dish_id])) {
$_SESSION['cart'][$dish_id] += $quantity;
} else {
$_SESSION['cart'][$dish_id] = $quantity;
}
header(Location: index.php);
exit;
}
?
✅ 5、`cart.php` — 购物车页面
php
<?php
session_start();
require_once 'db.php';
$total = 0;
$cart_items = [];
if (isset($_SESSION['cart'])) {
$dish_ids = array_keys($_SESSION['cart']);
$in_clause = str_repeat('?,', count($dish_ids) 1) 、'?';
$stmt = $pdo-prepare(SELECT id, name, price FROM dishes WHERE id IN ($in_clause));
$stmt-execute($dish_ids);
$dishes = $stmt-fetchAll();
foreach ($dishes as $dish) {
$cart_items[] = [
'id' = $dish['id'],
'name' = $dish['name'],
'price' = $dish['price'],
'quantity' = $_SESSION['cart'][$dish['id']]
];
$total += $dish['price'] * $_SESSION['cart'][$dish['id']];
}
}
?
<!DOCTYPE html
<html lang=zh
<head
<meta charset=UTF-8
<title购物车</title
<style
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid ddd; padding: 8px; text-align: left; }
.total { font-weight: bold; }
</style
</head
<body
<h1购物车</h1
<table
<thead
<tr
<th商品</th
<th单价</th
<th数量</th
<th小计</th
</tr
</thead
<tbody
<?php foreach ($cart_items as $item): ?
<tr
<td<?= htmlspecialchars($item['name']) ?</td
<td¥<?= number_format($item['price'], 2) ?</td
<td<?= $item['quantity'] ?</td
<td¥<?= number_format($item['price'] * $item['quantity'], 2) ?</td
</tr
<?php endforeach; ?
</tbody
</table
<p class=total总价:¥<?= number_format($total, 2) ?</p
<a href=order.php提交订单</a
<a href=index.php继续购物</a
</body
</html
✅ 6、`order.php` — 提交订单
php
<?php
session_start();
require_once 'db.php';
if (!isset($_SESSION['cart']) empty($_SESSION['cart'])) {
header(Location: cart.php);
exit;
}
// 模拟保存订单(实际中应存入订单表)
$order_details = [];
foreach ($_SESSION['cart'] as $dish_id = $quantity) {
$stmt = $pdo-prepare(SELECT name, price FROM dishes WHERE id = ?);
$stmt-execute([$dish_id]);
$dish = $stmt-fetch();
$order_details[] = [
'name' = $dish['name'],
'price' = $dish['price'],
'quantity' = $quantity
];
}
// 清空购物车
unset($_SESSION['cart']);
// 显示订单成功页面
?
<!DOCTYPE html
<html lang=zh
<head
<meta charset=UTF-8
<title订单成功</title
</head
<body
<h1订单提交成功!</h1
<p感谢您的光临,我们会尽快为您准备餐品。</p
<ul
<?php foreach ($order_details as $item): ?
<li<?= $item['name'] ? x<?= $item['quantity'] ? (¥<?= number_format($item['price'], 2) ?)</li
<?php endforeach; ?
</ul
<a href=index.php返回首页</a
</body
</html
✅ 7、使用说明
1、创建数据库:
bash
mysql -u root -p < dishes.sql
2、上传文件 到你的 Web 服务器(如 Apache + PHP)
3、访问 `index.php` 开始点餐
🔧 扩展建议
添加用户登录
支持删除购物车商品
订单管理后台
支付接口(支付宝/微信)
使用模板引擎(如 Twig)
使用 Composer 管理依赖
✅ 这是一个基础但完整的 PHP 点餐系统
📣 商家广告 :
👍
0
👎
IP地址: 141.85.1.9
搜索次数: 2
提问时间: 2025-11-05 22:41:36
🛒 域名购买 :
wangai.net koupin.com.cn 691.net.cn zhang.la 54.gold 2.js.cn 8.tj.cn 723.net.cn ftp.gs 0-7.com.cn
❓️ 热门提问 :
云服务器ecs与虚拟主机 本地搭建云主机 ip地址能精确到家吗 免费海外云主机 站群平台用户设置 境外服务器怎么租用 unt服务器 pbe服务器 核心机房建设方案 怎样打开ftp服务器 豌豆Ai站群搜索引擎系统
🌐 域名评估 :
oo.sn.cn le.sd.cn 72.gold uvde.cn huawei.sd.cn 5a.gd.cn taobao.com at.cq.cn x-j.net deepseek.center
⛏ 最新挖掘 :
凯里酸汤鱼 贵阳烤脑花 都匀毛尖茶 花溪牛肉粉 贵阳烤生蚝 贵阳烤豆腐 贵阳纸包鱼 贵阳特色菜
🖌 热门作画 :
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊 :
🔗 友情链接 :
検索エンジン
香港搜尋引擎排名
ai提问
🧰 站长工具 :Ai工具 whois查询 搜索
📢 温馨提示 :本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持 :本站由豌豆Ai 提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。