With the development of the Internet, website and application development are becoming more and more common, and more and more functions need to be implemented. The check-in function is a very basic but very popular function. For social networking websites and applications, the check-in function is a very important part. Let's take the PHP language as an example to explain how to implement the check-in function.
1. Preparation work
Before starting, we need to prepare the following work:
We are using the mysql database here. After opening the database, execute the following SQL statement:
CREATE DATABASE sign; USE sign; CREATE TABLE checkin( id MEDIUMINT NOT NULL AUTO_INCREMENT, user_id VARCHAR(16) NOT NULL, create_time DATETIME NOT NULL, PRIMARY KEY (id) )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Here we have created a database named sign, which contains a checkin table , used to store sign-in information. The checkin table contains three fields: id represents the unique identifier of the check-in record, user_id represents the ID of the check-in user, and create_time represents the check-in time.
2. Code Implementation
Next, we start writing the code for the check-in function. First we need to create an index.php file, the code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>签到</title> </head> <body> <?php if(isset($_POST['submit'])) { // 判断是否提交了表单 $userId = $_POST['user_id']; // 获取用户 ID $conn = mysqli_connect('localhost', 'root', '', 'sign'); // 连接数据库 if(!$conn) { die('连接数据库失败: ' . mysqli_error($conn)); // 判断连接是否成功 } $query = "INSERT INTO checkin (user_id, create_time) VALUES ('$userId', NOW())"; // SQL 插入语句 if(mysqli_query($conn, $query)) { // 判断插入是否成功 echo "<h2>签到成功!</h2>"; } else { echo "<h2>签到失败!</h2>"; } mysqli_close($conn); // 关闭connection } ?> <form method="post" action="index.php"> <label for="user_id">用户ID: </label> <input type="text" id="user_id" name="user_id"> <input type="submit" name="submit" value="签到"> </form> </body> </html>
Code idea:
3. Complete code
The following is the complete check-in function code (index.php file).
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>签到</title> </head> <body> <?php if(isset($_POST['submit'])) { // 判断是否提交了表单 $userId = $_POST['user_id']; // 获取用户 ID $conn = mysqli_connect('localhost', 'root', '', 'sign'); // 连接数据库 if(!$conn) { die('连接数据库失败: ' . mysqli_error($conn)); // 判断连接是否成功 } $query = "INSERT INTO checkin (user_id, create_time) VALUES ('$userId', NOW())"; // SQL 插入语句 if(mysqli_query($conn, $query)) { // 判断插入是否成功 echo "<h2>签到成功!</h2>"; } else { echo "<h2>签到失败!</h2>"; } mysqli_close($conn); // 关闭connection } ?> <form method="post" action="index.php"> <label for="user_id">用户ID: </label> <input type="text" id="user_id" name="user_id"> <input type="submit" name="submit" value="签到"> </form> </body> </html>
Code analysis:
means that if the form submits data, it will be executed after the form is submitted. logic (sign-in operation).
Get the user ID.
Connect to the database.
If the connection fails, the program execution will exit and a failure message will be prompted.
Add a new check-in record.
If the addition is successful, the user will be prompted to sign in successfully and a sign-in record will be added.
Close the database connection.
The above is the detailed content of Steps to implement the PHP check-in function (with code). For more information, please follow other related articles on the PHP Chinese website!