我可以为你提供一个详细的PHP商城源码示例,这个示例将包括基本的商城功能,如商品展示、购物车和订单处理。
目录结构
shopping_mall/ │ ├── index.php ├── product.php ├── cart.php ├── checkout.php ├── order.php └── db.php
数据库配置 (`db.php`)
我们需要一个数据库配置文件来连接数据库。
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "shopping_mall"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接 if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
首页 (`index.php`)
这是商城的首页,展示所有商品。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Shopping Mall</title> </head> <body> <h1>Welcome to the Shopping Mall</h1> <table border="1"> <tr> <th>Product ID</th> <th>Product Name</th> <th>Price</th> <th>Action</th> </tr> <?php include 'db.php'; $sql = "SELECT id, name, price FROM products"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>" . $row["id"] . "</td>"; echo "<td>" . $row["name"] . "</td>"; echo "<td>" . $row["price"] . "</td>"; echo "<td><a href='product.php?id=" . $row["id"] . "'>View Product</a></td>"; echo "</tr>"; } } else { echo "<tr><td colspan='4'>No products found</td></tr>"; } ?> </table> </body> </html>
商品详情页 (product.php
)
展示单个商品的详细信息,并提供添加到购物车的选项。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Product Details</title> </head> <body> <h1>Product Details</h1> <?php include 'db.php'; $product_id = $_GET['id']; $sql = "SELECT * FROM products WHERE id=$product_id"; $result = $conn->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); echo "<h2>" . $row["name"] . "</h2>"; echo "<p>Price: $" . $row["price"] . "</p>"; echo "<p>Description: " . $row["description"] . "</p>"; echo "<form action='cart.php' method='post'>"; echo "<input type='hidden' name='product_id' value='" . $row["id"] . "'>"; echo "<button type='submit'>Add to Cart</button>"; echo "</form>"; } else { echo "<p>Product not found</p>"; } ?> </body> </html>
购物车 (`cart.php`)
展示用户添加到购物车的商品,并提供结账选项。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Shopping Cart</title> </head> <body> <h1>Your Shopping Cart</h1> <?php session_start(); include 'db.php'; if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = []; } if (isset($_POST['product_id'])) { $product_id = $_POST['product_id']; $sql = "SELECT * FROM products WHERE id=$product_id"; $result = $conn->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $_SESSION['cart'][] = [ 'id' => $row['id'], 'name' => $row['name'], 'price' => $row['price'] ]; } } if (count($_SESSION['cart']) > 0) { echo "<table border='1'>"; echo "<tr><th>Product ID</th><th>Product Name</th><th>Price</th></tr>"; foreach ($_SESSION['cart'] as $item) { echo "<tr>"; echo "<td>" . $item['id'] . "</td>"; echo "<td>" . $item['name'] . "</td>"; echo "<td>" . $item['price'] . "</td>"; echo "</tr>"; } echo "</table>"; echo "<form action='checkout.php' method='post'>"; echo "<button type='submit'>Checkout</button>"; echo "</form>"; } else { echo "<p>Your cart is empty</p>"; } ?> </body> </html>
结账页面 (checkout.php
)
处理订单信息并生成订单。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Checkout</title> </head> <body> <h1>Checkout</h1> <?php session_start(); include 'db.php'; if (isset($_POST['submit'])) { if (count($_SESSION['cart']) > 0) { $total = 0; foreach ($_SESSION['cart'] as $item) { $total += $item['price']; } $sql = "INSERT INTO orders (total) VALUES ($total)"; if ($conn->query($sql) === TRUE) { $order_id = $conn->insert_id; foreach ($_SESSION['cart'] as $item) { $sql = "INSERT INTO order_items (order_id, product_id, quantity) VALUES ($order_id, $item['id'], 1)"; $conn->query($sql); } $_SESSION['cart'] = []; // Clear the cart after checkout echo "<p>Order placed successfully! Order ID: $order_id</p>"; } else { echo "<p>Error placing order: " . $conn->error . "</p>"; } } else { echo "<p>Your cart is empty</p>"; } } else { echo "<p>Total amount: $" . array_sum(array_column($_SESSION['cart'], 'price')) . "</p>"; echo "<form action='checkout.php' method='post'>"; echo "<button type='submit' name='submit'>Place Order</button>"; echo "</form>"; } ?> </body> </html>
订单页面 (`order.php`)
展示用户的订单历史。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Order History</title> </head> <body> <h1>Order History</h1> <?php include 'db.php'; $sql = "SELECT * FROM orders"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "<table border='1'>"; echo "<tr><th>Order ID</th><th>Total</th><th>Date</th></tr>"; while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>" . $row["id"] . "</td>"; echo "<td>" . $row["total"] . "</td>"; echo "<td>" . $row["date"] . "</td>"; echo "</tr>"; } echo "</table>"; } else { echo "<p>No orders found</p>"; } ?> </body> </html>
数据库表结构示例:
确保在MySQL中创建以下表:products
,orders
,order_items
,以下是SQL语句示例:
CREATE DATABASE shopping_mall; USE shopping_mall; CREATE TABLE products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, price DECIMAL(10, 2) NOT NULL, description TEXT NOT NULL ); INSERT INTO products (name, price, description) VALUES ('Product 1', 19.99, 'Description for product 1'); INSERT INTO products (name, price, description) VALUES ('Product 2', 29.99, 'Description for product 2'); INSERT INTO products (name, price, description) VALUES ('Product 3', 39.99, 'Description for product 3'); CREATE TABLE orders ( id INT AUTO_INCREMENT PRIMARY KEY, total DECIMAL(10, 2) NOT NULL, date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE order_items ( id INT AUTO_INCREMENT PRIMARY KEY, order_id INT NOT NULL, product_id INT NOT NULL, quantity INT NOT NULL, FOREIGN KEY (order_id) REFERENCES orders(id), FOREIGN KEY (product_id) REFERENCES products(id) );
各位小伙伴们,我刚刚为大家分享了有关php商城源码 _PHP的知识,希望对你们有所帮助。如果您还有其他相关问题需要解决,欢迎随时提出哦!
本文来源于互联网,如若侵权,请联系管理员删除,本文链接:https://www.9969.net/86612.html