How to implement employee leave management function through PHP?
Employee leave management is an important function in an enterprise. Effectively managing employees' leave applications can improve the company's work efficiency and employees' work enthusiasm. As a popular server-side scripting language, PHP is loved by developers because of its ease of learning, ease of use and flexibility. Through PHP, we can quickly implement employee leave management functions.
The following will introduce how to write an employee leave management system through PHP and provide specific code examples.
First, we need to design a database to store employees' leave information. Create a database named "leave_management" and create a table named "leave_requests" in it. The table should contain the following fields:
Create a leave application form on the web page to collect employees' leave information. The following is a simple example:
<form method="post" action="process_leave.php"> <label for="employee_name">员工姓名:</label> <input type="text" name="employee_name" id="employee_name" required> <label for="leave_type">请假类型:</label> <select name="leave_type" id="leave_type" required> <option value="病假">病假</option> <option value="事假">事假</option> <option value="年假">年假</option> </select> <label for="leave_start_date">请假开始日期:</label> <input type="date" name="leave_start_date" id="leave_start_date" required> <label for="leave_end_date">请假结束日期:</label> <input type="date" name="leave_end_date" id="leave_end_date" required> <label for="leave_reason">请假原因:</label> <textarea name="leave_reason" id="leave_reason" required></textarea> <input type="submit" value="提交申请"> </form>
Next, we create a file called "process_leave.php" for processing employee requests Leave application. In this file we will get the various fields from the form and insert them into the database.
<?php // 连接到数据库 $conn = mysqli_connect("localhost", "root", "password", "leave_management"); // 检查连接是否成功 if (!$conn) { die("数据库连接失败: " . mysqli_connect_error()); } // 获取表单中的字段值 $employee_name = $_POST['employee_name']; $leave_type = $_POST['leave_type']; $leave_start_date = $_POST['leave_start_date']; $leave_end_date = $_POST['leave_end_date']; $leave_reason = $_POST['leave_reason']; // 编写SQL语句并执行插入操作 $sql = "INSERT INTO leave_requests (employee_name, leave_type, leave_start_date, leave_end_date, leave_reason, status) VALUES ('$employee_name', '$leave_type', '$leave_start_date', '$leave_end_date', '$leave_reason', '待审批')"; if (mysqli_query($conn, $sql)) { echo "请假申请提交成功"; } else { echo "请假申请提交失败: " . mysqli_error($conn); } // 关闭数据库连接 mysqli_close($conn); ?>
In the web page, we can create a page to display employee leave information. The following is a simple example:
<?php // 连接到数据库 $conn = mysqli_connect("localhost", "root", "password", "leave_management"); // 检查连接是否成功 if (!$conn) { die("数据库连接失败: " . mysqli_connect_error()); } // 查询请假信息 $sql = "SELECT * FROM leave_requests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { echo "员工姓名: " . $row['employee_name'] . "<br>"; echo "请假类型: " . $row['leave_type'] . "<br>"; echo "请假开始日期: " . $row['leave_start_date'] . "<br>"; echo "请假结束日期: " . $row['leave_end_date'] . "<br>"; echo "请假原因: " . $row['leave_reason'] . "<br>"; echo "请假状态: " . $row['status'] . "<br><br>"; } } else { echo "暂无请假申请"; } // 关闭数据库连接 mysqli_close($conn); ?>
Through the above steps, we can implement a basic employee leave management function through PHP. When an employee submits a leave application, his or her leave information will be inserted into the database and can be viewed through another page.
Of course, we can expand and improve this basic system according to actual needs, such as adding approval functions, employee login restrictions, etc. I hope the code examples in this article can help you better understand how to implement employee leave management functions through PHP.
The above is the detailed content of How to implement employee leave management function through PHP?. For more information, please follow other related articles on the PHP Chinese website!