Home >Backend Development >PHP Tutorial >How to implement a simple online booking system using PHP
How to use PHP to implement a simple online reservation system
Introduction:
With the development of the Internet, online reservation systems have become a must in many industries Standard. This article will introduce how to use PHP language to implement a simple online reservation system and give corresponding code examples.
1. System requirements analysis
Our online booking system needs to have the following basic functions:
2. Database design
Create two tables in MySQL, one is the user table used to store user information, and the other is the hotel table used to store hotel information. The structure of the user information table is as follows:
CREATE TABLE user
(
id
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(50) NOT NULL,
email
varchar(50) NOT NULL,
password
varchar(50) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
The structure of the hotel information table is as follows:
CREATE TABLE hotel
(
id
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(50) NOT NULL,
address
varchar(100) NOT NULL,
price
decimal(10,2) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
3. System implementation
Code sample:
register.php:
<?php require 'db.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $name = $_POST['name']; $email = $_POST['email']; $password = password_hash($_POST['password'], PASSWORD_DEFAULT); $sql = "INSERT INTO user(name, email, password) VALUES('$name', '$email', '$password')"; mysqli_query($conn, $sql); header('Location: login.php'); exit; } ?> <form action="" method="post"> <input type="text" name="name" placeholder="用户名" required> <input type="email" name="email" placeholder="邮箱" required> <input type="password" name="password" placeholder="密码" required> <button type="submit">注册</button> </form>
login.php:
<?php require 'db.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $email = $_POST['email']; $password = $_POST['password']; $sql = "SELECT * FROM user WHERE email='$email'"; $result = mysqli_query($conn, $sql); $user = mysqli_fetch_assoc($result); if ($user && password_verify($password, $user['password'])) { session_start(); $_SESSION['user_id'] = $user['id']; header('Location: index.php'); exit; } else { echo '用户名或密码错误'; } } ?> <form action="" method="post"> <input type="email" name="email" placeholder="邮箱" required> <input type="password" name="password" placeholder="密码" required> <button type="submit">登录</button> </form>
Code sample:
index.php:
<?php require 'db.php'; $sql = "SELECT * FROM hotel"; $result = mysqli_query($conn, $sql); $hotels = mysqli_fetch_all($result, MYSQLI_ASSOC); ?> <?php foreach($hotels as $hotel): ?> <h3><?php echo $hotel['name']; ?></h3> <p>地址:<?php echo $hotel['address']; ?></p> <p>价格:<?php echo $hotel['price']; ?></p> <a href="booking.php?id=<?php echo $hotel['id']; ?>">预订</a> <?php endforeach; ?> booking.php: <?php session_start(); require 'db.php'; if (!isset($_SESSION['user_id'])) { header('Location: login.php'); exit; } $hotel_id = $_GET['id']; $user_id = $_SESSION['user_id']; $sql = "INSERT INTO booking(user_id, hotel_id) VALUES('$user_id', '$hotel_id')"; mysqli_query($conn, $sql); echo '预订成功'; ?>
Conclusion:
This article introduces how to use PHP language to implement a simple online reservation system, and gives corresponding code examples. Readers can make corresponding modifications and expansions according to their own needs to achieve more complete functions. Hope this article is helpful to you!
The above is the detailed content of How to implement a simple online booking system using PHP. For more information, please follow other related articles on the PHP Chinese website!