Home > Backend Development > PHP Tutorial > Steps to implement the PHP check-in function (with code)

Steps to implement the PHP check-in function (with code)

PHPz
Release: 2023-04-11 14:42:02
Original
1476 people have browsed it

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:

  1. Install an interpreter with PHP version 7.0 or above, such as XAMPP.
  2. Create a database and import the sign-in table.

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;
Copy after login

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[&#39;submit&#39;])) { // 判断是否提交了表单
        $userId = $_POST[&#39;user_id&#39;]; // 获取用户 ID
        $conn = mysqli_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;&#39;, &#39;sign&#39;); // 连接数据库
        if(!$conn) {
            die(&#39;连接数据库失败: &#39; . mysqli_error($conn)); // 判断连接是否成功
        }
        $query = "INSERT INTO checkin (user_id, create_time) VALUES (&#39;$userId&#39;, 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>
Copy after login
Copy after login

Code idea:

  1. When the user opens the check-in page, a form is displayed on the page, and there is an input box in the form And a submit button to enter the user ID and submit the check-in information.
  2. When the user enters the ID and clicks the submit button, the form will send a request to the server from the page to determine whether the user has signed in and save the sign-in information in the database.
  3. The last page displays the check-in results.

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[&#39;submit&#39;])) { // 判断是否提交了表单
        $userId = $_POST[&#39;user_id&#39;]; // 获取用户 ID
        $conn = mysqli_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;&#39;, &#39;sign&#39;); // 连接数据库
        if(!$conn) {
            die(&#39;连接数据库失败: &#39; . mysqli_error($conn)); // 判断连接是否成功
        }
        $query = "INSERT INTO checkin (user_id, create_time) VALUES (&#39;$userId&#39;, 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>
Copy after login
Copy after login

Code analysis:

  1. ##if(isset($_POST['submit'])) means that if the form submits data, it will be executed after the form is submitted. logic (sign-in operation).
  2. $userId = $_POST['user_id']; Get the user ID.
  3. $conn = mysqli_connect('localhost', 'root', '', 'sign'); Connect to the database.
  4. if(!$conn) If the connection fails, the program execution will exit and a failure message will be prompted.
  5. $query = "INSERT INTO checkin (user_id, create_time) VALUES ('$userId', NOW())"; Add a new check-in record.
  6. if(mysqli_query($conn, $query)) If the addition is successful, the user will be prompted to sign in successfully and a sign-in record will be added.
  7. mysqli_close($conn) Close the database connection.
4. Summary

So far, we have learned how to use PHP to implement the check-in function. Although the check-in function seems simple, it is not easy to implement. We need to clearly understand the entire check-in process and implement it one by one. Of course, in practical applications, more issues need to be considered, such as repeated user check-ins, check-in frequency limits, etc. These problems need to be solved according to different application scenarios.

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!

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