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)

Apr 03, 2023 pm 02:08 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles