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?
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
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
<?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
<?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
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!