处理请假的MySQL代码示例,要求再PHP前端操作后,数据表中以获得的假期天数加1,剩余天数减1
时间: 2024-03-28 16:41:39 浏览: 51
假设我们有一个名为`leave_records`的表,其中包含以下字段:
- `id`:假期记录的唯一ID
- `employee_id`:员工ID
- `start_date`:假期开始日期
- `end_date`:假期结束日期
- `total_days`:总假期天数
- `remaining_days`:剩余假期天数
下面是一个处理请假的MySQL代码示例:
```mysql
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "username", "password", "database");
// 获取员工ID和请假开始日期
$employee_id = $_POST['employee_id'];
$start_date = $_POST['start_date'];
// 查询剩余假期天数
$sql = "SELECT remaining_days FROM leave_records WHERE employee_id = '$employee_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$remaining_days = $row['remaining_days'];
if ($remaining_days > 0) {
// 更新请假记录
$end_date = $_POST['end_date'];
$total_days = $_POST['total_days'];
$sql = "INSERT INTO leave_records (employee_id, start_date, end_date, total_days, remaining_days)
VALUES ('$employee_id', '$start_date', '$end_date', '$total_days', '$remaining_days' - 1)";
mysqli_query($conn, $sql);
// 更新剩余假期天数
$sql = "UPDATE leave_records SET remaining_days = remaining_days - 1 WHERE employee_id = '$employee_id'";
mysqli_query($conn, $sql);
echo "请假申请已提交!";
} else {
echo "您没有剩余的假期天数了!";
}
// 关闭数据库连接
mysqli_close($conn);
?>
```
这段代码会首先从前端获取员工ID和请假开始日期,然后查询该员工的剩余假期天数。如果剩余天数大于0,则会将请假记录插入到`leave_records`表中,并更新该员工的剩余假期天数。如果剩余天数为0,则会提示用户无法提交请假申请。
阅读全文