Home >Backend Development >PHP Tutorial >How to use PHP to develop employee attendance data query tool?

How to use PHP to develop employee attendance data query tool?

王林
王林Original
2023-09-25 09:57:031363browse

How to use PHP to develop employee attendance data query tool?

How to use PHP to develop employee attendance data query tool?

Abstract: This article will introduce how to use PHP to develop a simple employee attendance data query tool. We will store employee attendance data through the MySQL database, and use PHP to write the query page and database connection code.

Keywords: PHP, employee attendance data, query tools, MySQL, database connection

1. Preparation work

  1. First, we need to install it in the local environment PHP and MySQL, make sure they are running properly.
  2. Create a MySQL database to store employee attendance data. You can use the following SQL statement to create a simple table to store data:
CREATE TABLE attendance (
    id INT PRIMARY KEY AUTO_INCREMENT,
    emp_id INT NOT NULL,
    date DATE NOT NULL,
    time_in TIME NOT NULL,
    time_out TIME,
    status ENUM('Present', 'Absent') NOT NULL
);

2. Write the database connection code

  1. In the root directory of the project, create a table named "dbconn.php" file. This file will be used for database connections and referenced in other files. In "dbconn.php", write the following code:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

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

Please replace "your_username", "your_password" and "your_database_name" with your MySQL connection credentials and database name.

3. Write the query page code

  1. Create a file named "index.php" in the project root directory as the employee attendance data query page. Write the following code:
<?php
include('dbconn.php');

$query = "SELECT * FROM attendance";
$result = $conn->query($query);
?>

<!DOCTYPE html>
<html>
<head>
    <title>员工考勤数据查询工具</title>
</head>
<body>
    <h1>员工考勤数据查询工具</h1>

    <table>
        <tr>
            <th>ID</th>
            <th>员工ID</th>
            <th>日期</th>
            <th>签到时间</th>
            <th>签退时间</th>
            <th>状态</th>
        </tr>

        <?php
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['emp_id'] . "</td>";
                echo "<td>" . $row['date'] . "</td>";
                echo "<td>" . $row['time_in'] . "</td>";
                echo "<td>" . $row['time_out'] . "</td>";
                echo "<td>" . $row['status'] . "</td>";
                echo "</tr>";
            }
        } else {
            echo "没有可用的数据";
        }
        ?>
    </table>

</body>
</html>

4. Run the query tool

  1. Save the above code and place the project in the root folder of your Web server.
  2. Enter your project URL in a web browser, such as "localhost/your_project_folder/index.php".
  3. You will see a simple employee attendance data query page, which displays the data retrieved from the database by the SELECT query.

Conclusion:
By following the steps in this article, you can develop a simple employee attendance data query tool using PHP. By modifying the database connection code and query page code, you can adapt it to any data set and need. I hope this article can help you quickly build an employee attendance data query tool.

The above is the detailed content of How to use PHP to develop employee attendance data query tool?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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