As businesses grow, human resource management becomes increasingly important, especially in managing employee leave. To improve efficiency and accuracy, many companies adopt electronic approval processes. This article explains how to implement an efficient employee leave approval process using PHP and provides specific code examples.
First, create a table in the MySQL database to store employee information (e.g., table name: employees), including employee ID, name, department, etc. Additionally, create a table to store leave information (e.g., table name: leaves), which should contain leave ID, employee ID, leave date, leave type, etc.
Next, create a PHP file named leave.php to handle the employee leave approval process. Add the database connection code at the top of the file to connect to the MySQL database.
<?php $servername = "localhost"; $username = "root"; $password = "your_password"; $dbname = "your_database"; // Create a database connection $conn = new mysqli($servername, $username, $password, $dbname); // Check if the connection is successful if ($conn->connect_error) { die("Database connection failed: " . $conn->connect_error); } // Set the character set to UTF-8 mysqli_set_charset($conn, 'utf8'); ?>
In the leave.php file, write a function called applyLeave() to handle leave applications. This function will receive the employee's leave details and insert them into the leaves table.
function applyLeave($employeeID, $leaveDate, $leaveType) { global $conn; $sql = "INSERT INTO leaves (employee_id, leave_date, leave_type) VALUES ('$employeeID', '$leaveDate', '$leaveType')"; if ($conn->query($sql) === TRUE) { echo "Leave application submitted"; } else { echo "Leave application failed"; } }
Next, write a function called approveLeave() to handle the leave approval process. This function will receive the supervisor's approval result and update the leave application's status.
function approveLeave($leaveID, $status) { global $conn; $sql = "UPDATE leaves SET status = '$status' WHERE leave_id = '$leaveID'"; if ($conn->query($sql) === TRUE) { echo "Approval result updated"; } else { echo "Failed to update approval result"; } }
We also need to write a function called getLeaveInfo() to query an employee's leave records. This function will query the leave information based on the employee ID.
function getLeaveInfo($employeeID) { global $conn; $sql = "SELECT * FROM leaves WHERE employee_id = '$employeeID'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "Leave ID: " . $row["leave_id"] . ", Employee ID: " . $row["employee_id"] . ", Leave Date: " . $row["leave_date"] . "<br>"; } } else { echo "No leave records found"; } }
Upload the leave.php file to your server and ensure it supports PHP. Then, access the file through a browser and use URL parameters to test different functions:
http://your_domain/leave.php?func=applyLeave&employeeID=1&leaveDate=2022-01-01&leaveType=Sick Leave
Use a similar URL to test the approval function:
http://your_domain/leave.php?func=approveLeave&leaveID=1&status=Approved
You can also use the following URL to query an employee's leave records:
http://your_domain/leave.php?func=getLeaveInfo&employeeID=1
By using PHP and MySQL, we can efficiently implement a complete employee leave approval process. Writing appropriate PHP functions to handle leave applications, approvals, and queries allows for effective management of employee leave data.