How to use PHP to develop online travel booking functions

PHPz
Release: 2023-08-25 10:24:01
Original
957 people have browsed it

How to use PHP to develop online travel booking functions

How to use PHP to develop online travel booking function

In today's highly digital era, more and more people choose to book travel services online. As a developer, knowing how to develop online travel booking functionality using PHP will be a competitive advantage. This article will introduce you to how to write a simple online travel booking system using PHP and provide some code examples.

Before you start, you need to have basic PHP programming knowledge and experience in operating MySQL database. If you already have the basics, let’s get started!

Step 1: Create a database

First, we need to create a database named "booking_system" and add the following two tables: users and bookings.

CREATE DATABASE booking_system;

USE booking_system;

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

CREATE TABLE bookings (
  id INT PRIMARY KEY AUTO_INCREMENT,
  user_id INT NOT NULL,
  destination VARCHAR(100) NOT NULL,
  arrival_date DATE NOT NULL,
  departure_date DATE NOT NULL,
  FOREIGN KEY (user_id) REFERENCES users(id)
);
Copy after login

Step 2: Create user registration and login functions

First, we need to create a registration form and a login form. In the registration form, users need to enter their name, email address and password. In the login form, users need to enter their email and password.

<!-- 注册表单 -->
<form action="register.php" 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>

<!-- 登录表单 -->
<form action="login.php" method="POST">
  <input type="email" name="email" placeholder="邮箱" required>
  <input type="password" name="password" placeholder="密码" required>
  <button type="submit">登录</button>
</form>
Copy after login

Next, we create a register.php file to handle the submission of the registration form and store the user information into the database.

<?php
// 连接到数据库
$con = mysqli_connect("localhost", "root", "password", "booking_system");

// 获取表单数据
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];

// 将用户信息插入到`users`表格中
$query = "INSERT INTO users (name, email, password) VALUES ('$name', '$email', '$password')";
mysqli_query($con, $query);

// 关闭数据库连接
mysqli_close($con);
?>
Copy after login

Similarly, we create a login.php file to handle the submission of the login form and verify whether the email and password entered by the user match the records in the database.

<?php
// 连接到数据库
$con = mysqli_connect("localhost", "root", "password", "booking_system");

// 获取表单数据
$email = $_POST['email'];
$password = $_POST['password'];

// 在`users`表格中查找匹配的记录
$query = "SELECT * FROM users WHERE email='$email' AND password='$password'";
$result = mysqli_query($con, $query);

if (mysqli_num_rows($result) == 1) {
  // 验证成功,用户登录
  echo "登录成功!";
} else {
  // 验证失败,提示用户错误信息
  echo "邮箱或密码不正确!";
}

// 关闭数据库连接
mysqli_close($con);
?>
Copy after login

Step 3: Create a reservation function

Now that we have completed the user registration and login functions, let's create a simple reservation function.

First, we create a booking form where the user enters the travel destination, arrival date, and departure date.

<!-- 预订表单 -->
<form action="booking.php" method="POST">
  <input type="text" name="destination" placeholder="目的地" required>
  <input type="date" name="arrival_date" required>
  <input type="date" name="departure_date" required>
  <button type="submit">预订</button>
</form>
Copy after login

Then, we create a booking.php file to handle the submission of the booking form and store the booking information into the database.

<?php
// 连接到数据库
$con = mysqli_connect("localhost", "root", "password", "booking_system");

// 获取表单数据
$destination = $_POST['destination'];
$arrival_date = $_POST['arrival_date'];
$departure_date = $_POST['departure_date'];

// 获取当前登录用户的ID
$user_id = $_SESSION['user_id'];

// 将预订信息插入到`bookings`表格中
$query = "INSERT INTO bookings (user_id, destination, arrival_date, departure_date) VALUES ($user_id, '$destination', '$arrival_date', '$departure_date')";
mysqli_query($con, $query);

// 关闭数据库连接
mysqli_close($con);
?>
Copy after login

So far, we have completed a simple online travel booking system. Through the introduction and sample code of this article, you should have a basic understanding of how to use PHP to develop online travel booking functions. Of course, this is just a simple example and you can extend and improve it according to your actual needs. I hope this article will be helpful to your learning and development!

The above is the detailed content of How to use PHP to develop online travel booking functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!