Home > Article > Backend Development > How to implement a simple online ordering system using PHP
How to use PHP to implement a simple online ordering system
With the popularity of the Internet, the ordering industry has gradually developed online. In order to meet the needs of users, the development of online ordering systems has become very important. This article will introduce how to use PHP language to implement a simple online ordering system and provide specific code examples.
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "dbname"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } ?>
<?php // 注册功能 if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["username"]) && !empty($_POST["password"])) { $username = $_POST["username"]; $password = $_POST["password"]; // 将用户名和密码存储到数据库中 $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; if ($conn->query($sql) === TRUE) { echo "注册成功"; } else { echo "注册失败:" . $conn->error; } } // 登录功能 if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["username"]) && !empty($_POST["password"])) { $username = $_POST["username"]; $password = $_POST["password"]; // 根据用户名和密码在数据库中进行查询 $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "登录成功"; } else { echo "登录失败"; } } ?>
<?php // 获取菜单信息 $sql = "SELECT * FROM menus"; $result = $conn->query($sql); // 显示菜单列表 if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "菜名:" . $row["name"]. " - 价格:" . $row["price"]. "<br>"; } } // 处理订单 if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["menu_id"]) && !empty($_POST["quantity"])) { $menu_id = $_POST["menu_id"]; $quantity = $_POST["quantity"]; $user_id = $_SESSION["user_id"]; // 将订单信息插入到数据库中 $sql = "INSERT INTO orders (user_id, menu_id, quantity, status) VALUES ('$user_id', '$menu_id', '$quantity', '0')"; if ($conn->query($sql) === TRUE) { echo "下单成功"; } else { echo "下单失败:" . $conn->error; } } ?>
The above code will obtain the menu information from the database and display the menu list to the user. When the user selects the menu and quantity and clicks the order button, the order information will be inserted into the database.
The above is an example of a simple online ordering system. By using PHP language, we can easily implement functions such as user registration and login, menu display, and order processing. Of course, this is just a basic example, and a real ordering system will be more complex, but this example can provide you with a good starting point. Good luck with your development!
The above is the detailed content of How to implement a simple online ordering system using PHP. For more information, please follow other related articles on the PHP Chinese website!