Home > Article > Backend Development > How to implement employee attendance approval record tracking through PHP?
How to track employee attendance approval records through PHP?
As modern enterprises continue to emphasize employee attendance management, many enterprises have gradually turned to electronic attendance management systems. In this kind of system, in order to facilitate the approval and tracking management of employees' attendance records, PHP, as a popular server-side scripting language, is widely used to develop such systems. This article will introduce how to implement the tracking function of employee attendance approval records through PHP, and provide relevant code examples.
1. Database design
First, we need to design a database to store employee attendance-related information. The following is a simple database table design example:
CREATE TABLE `attendance` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `employee_id` INT(11) NOT NULL, `date` DATE NOT NULL, `status` ENUM('Pending', 'Approved', 'Rejected') NOT NULL DEFAULT 'Pending', PRIMARY KEY (`id`) );
In the above table, we use a table named attendance
to record the attendance record of each employee. Among them, id
is used as the primary key to uniquely identify each record, employee_id
is used to store the employee's ID, date
is used to record the attendance date, status
Used to record the approval status of the attendance record, including Pending, Approved and Rejected.
2. PHP code implementation
First, we need to connect to the database through PHP code. The following is a simple database connection function example:
<?php $servername = "localhost"; $username = "your_username"; $password = "your_password"; $dbname = "your_database"; // 创建数据库连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("数据库连接失败: " . $conn->connect_error); } ?>
Please replace your_username
, your_password
and your_database
in the example with your actual database Username, password and database name.
Next, we need to write PHP code to query employee attendance records and display the results on the page. The following is a simple code example for querying attendance records:
<?php // 查询考勤记录 $sql = "SELECT * FROM attendance"; $result = $conn->query($sql); // 判断查询结果是否为空 if ($result->num_rows > 0) { // 输出数据 while($row = $result->fetch_assoc()) { echo "员工ID:" . $row["employee_id"]. " - 日期:" . $row["date"]. " - 状态:" . $row["status"]. "<br>"; } } else { echo "暂无考勤记录"; } ?>
When approving employee attendance records, we need to write PHP code to update attendance The record's approval status. The following is a simple code example for updating attendance records:
<?php // 更新考勤记录的审批状态 $attendance_id = 1; // 要更新的考勤记录ID $status = "Approved"; // 更新后的审批状态 $sql = "UPDATE attendance SET status='$status' WHERE id=$attendance_id"; if ($conn->query($sql) === TRUE) { echo "考勤记录更新成功"; } else { echo "考勤记录更新失败: " . $conn->error; } ?>
The above example updates the approval status of the record with attendance record ID 1 to "Approved". You can modify the values of $attendance_id
and $status
according to the actual situation.
3. Summary
Through the implementation of the above PHP code, we can easily implement the tracking function of employee attendance approval records. Using database storage and PHP implementation, we can query and update attendance records, and display approval results as needed. Of course, this is just a simple example, and the actual attendance management system may require more functions and details to meet the specific needs of the enterprise. Hope this article is helpful to you.
The above is the detailed content of How to implement employee attendance approval record tracking through PHP?. For more information, please follow other related articles on the PHP Chinese website!