
How to use PHP to implement a simple online order management system
1. Introduction
The online order management system is a common e-commerce application , which can help merchants effectively manage the order process, speed up order processing, and improve customer satisfaction. This article will introduce how to use PHP to implement a simple online order management system, including the creation, modification, query and deletion of orders. This article assumes that readers already have some basic knowledge of PHP.
2. System requirements
The online order management system needs to meet the following basic requirements:
- Login function: Administrators can log in to the system through username and password.
- Order management function: Administrators can create, modify, query and delete orders.
- Order list function: Administrators can view the list of orders, filter and sort by conditions.
- Order details function: The administrator can view the detailed information of the order, including order number, customer information, product information, etc.
- Database support: The system needs to use the MySQL database to store order data.
3. Implementation steps
- Create database
First, we need to create a database to store order data. Use the following SQL statement to create a database named "orders" and create a data table named "order_info" to store order information.
CREATE DATABASE orders;
USE orders;
CREATE TABLE order_info (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
order_number VARCHAR(20) NOT NULL,
customer_name VARCHAR(50) NOT NULL,
total_price DECIMAL(10,2) NOT NULL,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
- Create a login page
Create a file named "index.php" in the root directory of the project and add the following code:
<?php
session_start();
if(isset($_POST['username']) && isset($_POST['password'])){
if($_POST['username'] === 'admin' && $_POST['password'] === 'admin'){
$_SESSION['login'] = true;
header('Location: order_list.php');
exit;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>登錄</title>
<meta charset="UTF-8">
</head>
<body>
<form method="POST" action="index.php">
<input type="text" name="username" placeholder="用戶名">
<input type="password" name="password" placeholder="密碼">
<button type="submit">登錄</button>
</form>
</body>
</html>
This page contains a simple login form. When the username and password are correct, the login status will be saved in the session and jump to the order list page.
- Create order list page
Create a file named "order_list.php" in the root directory of the project and add the following code:
<?php
session_start();
if(!isset($_SESSION['login']) || $_SESSION['login'] !== true){
header('Location: index.php');
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>訂單列表</title>
<meta charset="UTF-8">
</head>
<body>
<h1>訂單列表</h1>
<a href="create_order.php">創(chuàng)建新訂單</a>
<table>
<tr>
<th>訂單號(hào)</th>
<th>客戶姓名</th>
<th>訂單金額</th>
<th>創(chuàng)建時(shí)間</th>
<th></th>
</tr>
<?php
// 連接數(shù)據(jù)庫
$conn = new mysqli('localhost', 'root', 'password', 'orders');
if ($conn->connect_error) {
die('數(shù)據(jù)庫連接失敗:' . $conn->connect_error);
}
// 查詢訂單列表
$sql = 'SELECT * FROM order_info ORDER BY create_time DESC';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td>'.$row['order_number'].'</td>';
echo '<td>'.$row['customer_name'].'</td>';
echo '<td>'.$row['total_price'].'</td>';
echo '<td>'.$row['create_time'].'</td>';
echo '<td><a href="order_details.php?id='.$row['id'].'">詳情</a></td>';
echo '</tr>';
}
} else {
echo '<tr><td colspan="5">暫無訂單信息</td></tr>';
}
$conn->close();
?>
</table>
</body>
</html>
This page first checks the login status. If the user is not logged in, it jumps to the login page. Next, it queries the order list from the database and displays it in an HTML table. Each row displays the order number, customer name, order amount, creation time, and a link to view the details of the order.
- Create order details page
Create a file named "order_details.php" in the root directory of the project and add the following code:
<?php
session_start();
if(!isset($_SESSION['login']) || $_SESSION['login'] !== true){
header('Location: index.php');
exit;
}
if(!isset($_GET['id'])){
header('Location: order_list.php');
exit;
}
$id = $_GET['id'];
// 連接數(shù)據(jù)庫
$conn = new mysqli('localhost', 'root', 'password', 'orders');
if ($conn->connect_error) {
die('數(shù)據(jù)庫連接失?。? . $conn->connect_error);
}
// 查詢訂單詳情
$sql = 'SELECT * FROM order_info WHERE id = '.$id;
$result = $conn->query($sql);
if ($result->num_rows == 0) {
header('Location: order_list.php');
exit;
}
$row = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html>
<head>
<title>訂單詳情</title>
<meta charset="UTF-8">
</head>
<body>
<h1>訂單詳情</h1>
<p><strong>訂單號(hào):</strong><?php echo $row['order_number'];?></p>
<p><strong>客戶姓名:</strong><?php echo $row['customer_name'];?></p>
<p><strong>訂單金額:</strong><?php echo $row['total_price'];?></p>
<p><strong>創(chuàng)建時(shí)間:</strong><?php echo $row['create_time'];?></p>
<a href="edit_order.php?id=<?php echo $row['id'];?>">編輯訂單</a>
<a href="delete_order.php?id=<?php echo $row['id'];?>" onclick="return confirm('確認(rèn)刪除該訂單嗎?')">刪除訂單</a>
</body>
</html>
This page first checks the login status and order id. If the user is not logged in or does not pass the order id, it will jump to the order list page. Next, it queries the order details from the database and displays them in an HTML page. The page also contains links to edit orders and delete orders.
- Create create order page
Create a file named "create_order.php" in the root directory of the project and add the following code:
<?php
session_start();
if(!isset($_SESSION['login']) || $_SESSION['login'] !== true){
header('Location: index.php');
exit;
}
if(isset($_POST['submit'])){
// 獲取訂單信息
$order_number = $_POST['order_number'];
$customer_name = $_POST['customer_name'];
$total_price = $_POST['total_price'];
// 連接數(shù)據(jù)庫
$conn = new mysqli('localhost', 'root', 'password', 'orders');
if ($conn->connect_error) {
die('數(shù)據(jù)庫連接失?。? . $conn->connect_error);
}
// 創(chuàng)建訂單
$sql = 'INSERT INTO order_info (order_number, customer_name, total_price) VALUES ("'.$order_number.'", "'.$customer_name.'", '.$total_price.')';
$result = $conn->query($sql);
if($result === true){
header('Location: order_list.php');
exit;
} else {
die('創(chuàng)建訂單失?。? . $conn->error);
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>創(chuàng)建訂單</title>
<meta charset="UTF-8">
</head>
<body>
<h1>創(chuàng)建訂單</h1>
<form method="POST" action="create_order.php">
<input type="text" name="order_number" placeholder="訂單號(hào)">
<input type="text" name="customer_name" placeholder="客戶姓名">
<input type="number" step="0.01" name="total_price" placeholder="訂單金額">
<button type="submit" name="submit">創(chuàng)建訂單</button>
</form>
</body>
</html>
This page first checks the login status. If the user is not logged in, it jumps to the login page. It then saves the order information to the database when the order form is submitted.
- Create editing order page and delete order function
The implementation of editing order function is similar to creating order. Just add the following code to the "edit_order.php" page:
<?php
session_start();
if(!isset($_SESSION['login']) || $_SESSION['login'] !== true){
header('Location: index.php');
exit;
}
if(!isset($_GET['id'])){
header('Location: order_list.php');
exit;
}
$id = $_GET['id'];
// 連接數(shù)據(jù)庫
$conn = new mysqli('localhost', 'root', 'password', 'orders');
if ($conn->connect_error) {
die('數(shù)據(jù)庫連接失?。? . $conn->connect_error);
}
// 查詢訂單詳情
$sql = 'SELECT * FROM order_info WHERE id = '.$id;
$result = $conn->query($sql);
if ($result->num_rows == 0) {
header('Location: order_list.php');
exit;
}
$row = $result->fetch_assoc();
if(isset($_POST['submit'])){
// 獲取訂單信息
$order_number = $_POST['order_number'];
$customer_name = $_POST['customer_name'];
$total_price = $_POST['total_price'];
// 更新訂單
$sql = 'UPDATE order_info SET order_number = "'.$order_number.'", customer_name = "'.$customer_name.'", total_price = '.$total_price.' WHERE id = '.$id;
$result = $conn->query($sql);
if($result === true){
header('Location: order_details.php?id='.$id);
exit;
} else {
die('編輯訂單失?。? . $conn->error);
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>編輯訂單</title>
<meta charset="UTF-8">
</head>
<body>
<h1>編輯訂單</h1>
<form method="POST" action="edit_order.php?id=<?php echo $row['id'];?>">
<input type="text" name="order_number" placeholder="訂單號(hào)" value="<?php echo $row['order_number'];?>">
<input type="text" name="customer_name" placeholder="客戶姓名" value="<?php echo $row['customer_name'];?>">
<input type="number" step="0.01" name="total_price" placeholder="訂單金額" value="<?php echo $row['total_price'];?>">
<button type="submit" name="submit">保存修改</button>
</form>
</body>
</html>
The implementation of the delete order function is similar, just add the following code to the "delete_order.php" page:
<?php
session_start();
if(!isset($_SESSION['login']) || $_SESSION['login'] !== true){
header('Location: index.php');
exit;
}
if(!isset($_GET['id'])){
header('Location: order_list.php');
exit;
}
$id = $_GET['id'];
// 連接數(shù)據(jù)庫
$conn = new mysqli('localhost', 'root', 'password', 'orders');
if ($conn->connect_error) {
die('數(shù)據(jù)庫連接失敗:' . $conn->connect_error);
}
// 刪除訂單
$sql = 'DELETE FROM order_info WHERE id = '.$id;
$result = $conn->query($sql);
if($result === true){
header('Location: order_list.php');
exit;
} else {
die('刪除訂單失?。? . $conn->error);
}
$conn->close();
?>
This page first checks the login status and order id. If the user is not logged in or If the order id is not passed, it will jump to the order list page. Then, it deletes the order with the specified id from the database and jumps to the order list page.
IV. Summary
This article introduces how to use PHP to implement a simple online order management system, including the creation, modification, query and deletion of orders. Through the sample code in this article, readers can learn how to use PHP and MySQL to implement a simple e-commerce application to provide merchants with a more efficient order management method. Of course, this is just a basic example, and more functions and security need to be considered in actual applications. I hope this article is helpful to readers, thank you for reading!
The above is the detailed content of How to implement a simple online order management system using PHP. For more information, please follow other related articles on the PHP Chinese website!