如何让请假表单的pending状态自动显示?已设表默认值仍无效
Hey there! Let's figure out why your pending status isn't showing up in the leave application list. Based on the details you shared, here are some key areas to check and fix:
1. Verify Database Default Value is Working Correctly
First, let's confirm new leave requests are actually getting the pending status stored in the database. Run this SQL query directly on your database to check the latest records:
SELECT id, full_name, status FROM leaves ORDER BY id DESC LIMIT 10;
- If the
statuscolumn showsNULLor an empty string for new entries, your default value isn't being applied properly. - Double-check that the default value is set as a string (e.g.,
'pending'with single quotes) and not a bare word or NULL.
2. Check the status Field's Data Type
Make sure your status field is configured correctly in the table:
- If it's a
VARCHARtype: Ensure the default value is exactly'pending'(no extra spaces, correct lowercase/uppercase match—case matters unless your database is set to case-insensitive). - If it's an
ENUMtype: Confirm that'pending'is one of the defined enum values. If not, inserting a record without specifyingstatuswill fall back to the first enum value (or NULL if no default is set for the enum).
3. Debug the Form Submission Logic
Even if you set a database default, your form submission code might be overwriting it. Check the PHP code that handles leave request submissions:
- Look for lines where you insert data into the
leavestable. If you're explicitly settingstatusto an empty value orNULL(e.g.,$status = $_POST['status']but the form doesn't include a status field), that will override the database default. - Ensure you're not including
statusin the INSERT statement if you want the database default to take effect, or explicitly set it to'pending'in the insert code.
4. Add Debugging to the View Page
Modify your view-leave code to see exactly what's being pulled from the database for the status field. Replace the status output line with this:
<td> <?php // Debug: Show raw value and type var_dump($row_message['status']); // Fallback for empty/null status echo !empty($row_message['status']) ? $row_message['status'] : 'Pending'; ?> </td>
This will show you if the value is NULL, empty, or a different string than expected. If it's NULL, the issue is definitely with how the status is being stored in the database.
5. Check for Query Errors
Add error handling to your view page's database query to rule out any unexpected issues:
<?php include ('database.php'); $result = $database->prepare ("SELECT * FROM leaves order by id DESC"); $executeSuccess = $result->execute(); // Check for execution errors if (!$executeSuccess) { echo "Query Error: "; print_r($result->errorInfo()); } for ($count=0; $row_message = $result->fetch(); $count++){ ?>
Once you've checked these areas, you should be able to pinpoint whether the problem is in the database configuration, submission logic, or view rendering. Let me know what you find and we can tweak further if needed!
内容的提问来源于stack exchange,提问作者Renzo

