Home >Database >Mysql Tutorial >MySQL implements the reservation function of the ordering system

MySQL implements the reservation function of the ordering system

WBOY
WBOYOriginal
2023-11-01 16:16:471178browse

MySQL 实现点餐系统的预定功能

MySQL implements the reservation function of the ordering system and requires specific code examples

With the advancement of technology and the acceleration of people's pace of life, more and more people choose Restaurant reservations through the ordering system have become a standard feature in the modern catering industry. This article will introduce how to use the MySQL database to implement the reservation function of a simple ordering system and provide specific code examples.

When designing the reservation function of the ordering system, we first need to determine the structure of the database. In MySQL, a relational database can be used to store related data. The following is a simple database structure, including three tables: users, restaurants, and bookings.

  • users table is used to store user information, including the user’s unique identifier (id) and user name (username) and password (password), etc. The
  • restaurants table is used to store restaurant information, including the restaurant’s unique identifier (id), restaurant name (name) and address ( address) etc. The
  • bookings table is used to store user reservation information, including the unique identifier of the reservation (id), user ID (user_id), Restaurant ID (restaurant_id), reservation time (booking_time), number of reservations (party_size), etc.

The following is a MySQL code example to create the above three tables:

CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(50) NOT NULL
);

CREATE TABLE restaurants (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    address VARCHAR(200) NOT NULL
);

CREATE TABLE bookings (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    restaurant_id INT NOT NULL,
    booking_time DATETIME NOT NULL,
    party_size INT NOT NULL,
    FOREIGN KEY (user_id) REFERENCES users(id),
    FOREIGN KEY (restaurant_id) REFERENCES restaurants(id)
);

After completing the design of the database, we can implement the reservation function of the ordering system by writing some simple code . The following is a sample code written in Python for adding reservation information to the database:

import mysql.connector

# 建立数据库连接
db = mysql.connector.connect(
    host="localhost",
    user="root",
    password="password",
    database="restaurant_booking"
)

# 创建游标对象
cursor = db.cursor()

# 获取用户输入的预定信息
username = input("请输入用户名:")
password = input("请输入密码:")
restaurant_id = input("请输入餐厅ID:")
booking_time = input("请输入预定时间(YYYY-MM-DD HH:MM:SS):")
party_size = input("请输入预定人数:")

# 查询用户ID
cursor.execute(f"SELECT id FROM users WHERE username = '{username}' AND password = '{password}'")
user_id = cursor.fetchone()
if not user_id:
    print("用户名或密码错误")
    exit()

# 添加预定信息到数据库
cursor.execute(f"INSERT INTO bookings (user_id, restaurant_id, booking_time, party_size) VALUES ({user_id[0]}, {restaurant_id}, '{booking_time}', {party_size})")
db.commit()

print("预定成功")

# 关闭数据库连接
db.close()

The above code example demonstrates how to add the user's reservation information to the database. The user needs to enter the username, password, restaurant ID, reservation time and number of reservations. The program will first verify the user's identity and add the reservation information to the bookings table.

Through the above MySQL database structure and code examples, we can implement the reservation function of a simple ordering system. Of course, the actual ordering system may also include other more complex functions, such as viewing reservation records, canceling reservations, etc. But through this simple demonstration, we can provide readers with a basic idea and reference to help them better understand how to use MySQL to implement the reservation function of the ordering system.

The above is the detailed content of MySQL implements the reservation function of the ordering system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn