How to use PHP to develop online reservation function

PHPz
Release: 2023-08-18 21:08:01
Original
907 people have browsed it

How to use PHP to develop online reservation function

How to use PHP to develop online reservation function

Overview:
With the rapid development of the Internet, more and more businesses begin to migrate online, and reservations Function has also become one of the necessary functions for many websites and APPs. This article will introduce how to use PHP to develop a simple online reservation function and provide corresponding code examples.

1. Establish database and table structure

First, we need to create a table in the MySQL database to store reservation information. Suppose our table is named "appointments" and has the following fields:

  • id - the unique identifier of the appointment record
  • name - the name of the person who made the appointment
  • email - Reservation person’s email
  • phone - Reservation person’s phone number
  • date - Reservation date
  • time - Reservation time
  • create_time - Reservation record creation time

You can use the following SQL statement to create this table:

CREATE TABLE appointments (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(50) NOT NULL,
    phone VARCHAR(15) NOT NULL,
    date DATE NOT NULL,
    time TIME NOT NULL,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Copy after login

2. Create an appointment form and submit the processing logic

In the HTML page, we can create a simple form, Used to fill in reservation information. On form submission, we will use PHP to process the submitted data and save it to the database. The following is a sample HTML and PHP code:

<!-- index.html -->
<form method="post" action="submit.php">
    <label for="name">姓名:</label>
    <input type="text" name="name" required>
    <br>
    <label for="email">邮箱:</label>
    <input type="email" name="email" required>
    <br>
    <label for="phone">电话:</label>
    <input type="text" name="phone" required>
    <br>
    <label for="date">日期:</label>
    <input type="date" name="date" required>
    <br>
    <label for="time">时间:</label>
    <input type="time" name="time" required>
    <br>
    <input type="submit" value="提交预约">
</form>
Copy after login
// submit.php
<?php
// 连接数据库
$conn = new mysqli("localhost", "username", "password", "database");

// 检查连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 获取表单提交的数据
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$date = $_POST["date"];
$time = $_POST["time"];

// 插入预约信息到数据库
$sql = "INSERT INTO appointments (name, email, phone, date, time) VALUES ('$name', '$email', '$phone', '$date', '$time')";

if ($conn->query($sql) === true) {
    echo "预约成功!";
} else {
    echo "预约失败: " . $conn->error;
}

// 关闭数据库连接
$conn->close();
?>
Copy after login

In the above code, we first connect to the database in the submit.php file and obtain various information submitted by the form. We then use SQL statements to insert this information into the reservation table. If the insertion is successful, "Reservation successful!" will be output; otherwise, the corresponding error message will be output.

3. Display reservation information

In the reservation function, we generally need a page to display the submitted reservation information. The following is a simple PHP code example for querying and displaying reservation information from the database:

// appointment-list.php
<?php
// 连接数据库
$conn = new mysqli("localhost", "username", "password", "database");

// 检查连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 查询预约信息
$sql = "SELECT * FROM appointments";
$result = $conn->query($sql);

// 输出预约信息
if ($result->num_rows > 0) {
    echo "<table>
            <tr>
                <th>ID</th>
                <th>姓名</th>
                <th>邮箱</th>
                <th>电话</th>
                <th>日期</th>
                <th>时间</th>
                <th>创建时间</th>
            </tr>";
    while ($row = $result->fetch_assoc()) {
        echo "<tr>
                <td>".$row["id"]."</td>
                <td>".$row["name"]."</td>
                <td>".$row["email"]."</td>
                <td>".$row["phone"]."</td>
                <td>".$row["date"]."</td>
                <td>".$row["time"]."</td>
                <td>".$row["create_time"]."</td>
            </tr>";
    }
    echo "</table>";
} else {
    echo "暂无预约信息";
}

// 关闭数据库连接
$conn->close();
?>
Copy after login

The above code will query the reservation information in the database and output it to a simple HTML table.

Summary:

Through the introduction of this article, we have mastered the basic process of using PHP to develop online reservation functions. First, create a reservation table in the database, and use HTML and PHP to create the reservation form and submission processing logic. We can then create a page to display the submitted reservation information. Of course, this is just a simple example. You can expand and optimize the code based on actual needs to make it more in line with actual business needs.

The above is the detailed content of How to use PHP to develop online reservation function. 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!