Home >Backend Development >PHP Tutorial >How to use PHP to implement the hotel booking function of WeChat applet?
How to use PHP to implement the hotel reservation function of WeChat applet?
With the rise of WeChat mini programs, more and more companies are beginning to use WeChat mini programs to provide hotel booking services. As a widely used server-side programming language, PHP is an excellent choice for implementing hotel reservation functions. The following will introduce in detail how to use PHP to implement the hotel booking function of the WeChat applet and provide specific code examples.
CREATE TABLE hotels ( id INT(11) PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, address VARCHAR(255) NOT NULL, description TEXT, price DECIMAL(10, 2) NOT NULL ); CREATE TABLE bookings ( id INT(11) PRIMARY KEY AUTO_INCREMENT, hotel_id INT(11) NOT NULL, check_in_date DATE NOT NULL, check_out_date DATE NOT NULL, guest_name VARCHAR(100) NOT NULL, guest_email VARCHAR(100) NOT NULL, FOREIGN KEY (hotel_id) REFERENCES hotels(id) ON DELETE CASCADE );
<?php $db_host = "localhost"; $db_username = "your_db_username"; $db_password = "your_db_password"; $db_name = "your_db_name"; $wx_app_id = "your_app_id"; $wx_app_secret = "your_app_secret";
<?php require_once 'config.php'; $conn = new mysqli($db_host, $db_username, $db_password, $db_name); if ($conn->connect_errno) { die("Failed to connect to MySQL: " . $conn->connect_error); }
<?php require_once 'db.php'; $result = $conn->query("SELECT * FROM hotels"); if ($result->num_rows > 0) { $hotels = array(); while ($row = $result->fetch_assoc()) { $hotels[] = $row; } echo json_encode($hotels); } else { echo json_encode(array()); } $conn->close();
<?php require_once 'db.php'; $hotel_id = $_POST['hotel_id']; $check_in_date = $_POST['check_in_date']; $check_out_date = $_POST['check_out_date']; $guest_name = $_POST['guest_name']; $guest_email = $_POST['guest_email']; $stmt = $conn->prepare("INSERT INTO bookings (hotel_id, check_in_date, check_out_date, guest_name, guest_email) VALUES (?, ?, ?, ?, ?)"); $stmt->bind_param("issss", $hotel_id, $check_in_date, $check_out_date, $guest_name, $guest_email); if ($stmt->execute()) { echo "Booking created successfully"; } else { echo "Failed to create booking"; } $stmt->close(); $conn->close();
// 获取酒店列表 wx.request({ url: 'https://your-domain.com/index.php', success: function(res) { console.log(res.data); // TODO: 处理酒店列表数据 } }); // 创建酒店预订 wx.request({ url: 'https://your-domain.com/create_booking.php', method: 'POST', data: { hotel_id: 1, check_in_date: '2021-01-01', check_out_date: '2021-01-05', guest_name: 'John Doe', guest_email: 'johndoe@example.com' }, success: function(res) { console.log(res.data); // TODO: 处理预订结果数据 } });
Through the above steps, you can use PHP to implement the hotel booking function of the WeChat applet. Of course, there are many details to consider in actual development, such as data verification, user login, etc. I hope this article can provide you with some guidance, and I wish you success in completing the hotel reservation function of the WeChat mini program!
The above is the detailed content of How to use PHP to implement the hotel booking function of WeChat applet?. For more information, please follow other related articles on the PHP Chinese website!