Home  >  Article  >  Backend Development  >  How to implement a simple online booking system using PHP

How to implement a simple online booking system using PHP

王林
王林Original
2023-09-25 11:12:36745browse

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:

  1. User registration and login: Users can register an account and log in through to manage and book related information.
  2. Hotel list display: Users can browse the hotel list in the system and view the corresponding hotel information.
  3. Book a hotel: Users can choose their favorite hotel and make a reservation. At the same time, the system needs to record the hotel information booked by the user.
  4. Order management: Users can view their order information and perform corresponding management operations (such as canceling and modifying orders).
  5. Backend management: The administrator needs to be able to manage the hotel and order information in the system, and perform statistics and report generation of corresponding data.

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

  1. Registration and login function:

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>
  1. Hotel list display and booking function:

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 '预订成功';
?>
  1. Order management function: Omitted, you can query the order information from the booking table according to the user ID, and perform corresponding management operations.
  2. Backend management function: Omitted, you can manage and count hotel and order information after logging in with the administrator account.

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!

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