PHP语法错误排查:员工休假审批状态更新功能异常
Fixing the Parse Error & Implementing Status Updates for Leave Approval
Hey there, let's break down and fix your issues one by one:
1. Direct Cause of the Parse Error
The immediate syntax error comes from missing semicolons at the end of these lines in your PHP code:
$status=$_POST['status']
PHP requires semicolons to terminate statements, so adding ; to both instances will resolve the "unexpected '}'" error right away.
2. Other Critical Issues to Make Status Updates Work
Your current code doesn't actually update the database (so the Status column won't reflect changes), plus there are small typos and missing form data:
- The "Reject" button has a typo:
typ="submit"should betype="submit" - Your forms don't pass the specific leave ID, so we can't target which leave entry to update
- There's no logic to write the approval/rejection status to your
leavestable
Fixed Full Code
Here's the corrected version that addresses all these issues and makes the status update functional:
<body> <?php include ('database.php'); // Handle approval/rejection submissions if(isset($_POST['approved'])) { $leave_id = $_POST['leave_id']; $status = "Approved"; // Update the database with prepared statement (safer against SQL injection) $update = $database->prepare("UPDATE leaves SET status = ? WHERE id = ?"); $update->execute([$status, $leave_id]); } if(isset($_POST['rejected'])) { $leave_id = $_POST['leave_id']; $status = "Rejected"; $update = $database->prepare("UPDATE leaves SET status = ? WHERE id = ?"); $update->execute([$status, $leave_id]); } ?> <!-- Begin page content --> <div class="container"> <div class="page-header"> <h3> Employee Leaves </h3> <div class="table-responsive"> <table class="table"> <tr> <th>Employee Name</th> <th>Phone</th> <th>Email</th> <th>From</th> <th>To</th> <th>Reason</th> <th>Status</th> <th>---</th> </tr> <?php $result = $database->prepare("SELECT * FROM leaves order by id DESC"); $result->execute(); // Using while loop is cleaner for fetching rows from database while($row_message = $result->fetch()){ ?> <tr> <td><?php echo $row_message['full_name']; ?></td> <td><?php echo $row_message['phone']; ?></td> <td><?php echo $row_message['email']; ?></td> <td><?php echo $row_message['fromdate']; ?></td> <td><?php echo $row_message['todate']; ?></td> <td><?php echo $row_message['reason']; ?></td> <td><?php echo $row_message['status']; ?></td> <td> <form method="post" action=""> <!-- Hidden input to pass the unique leave ID --> <input type="hidden" name="leave_id" value="<?php echo $row_message['id']; ?>"> <button type="submit" name="approved">Approve</button> </form> <form method="post" action=""> <input type="hidden" name="leave_id" value="<?php echo $row_message['id']; ?>"> <button type="submit" name="rejected">Reject</button> </form> </td> </tr> <?php } ?> </table> <a href="home"><button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button></a> </div> </div> </div> </body>
Key Changes Made:
- Added semicolons to fix the initial syntax error
- Added hidden input fields to pass the
leave_idso we can target specific leave entries - Added database update logic using prepared statements (safer and more secure)
- Fixed the typo on the Reject button
- Swapped the
forloop with awhileloop for more intuitive row fetching - Set explicit status values ("Approved"/"Rejected") to ensure consistency in the database
内容的提问来源于stack exchange,提问作者John Z
相关产品推荐
相关产品推荐

