如何将HTML input type=time的内容插入MySQL数据库?
Hey there! I see you've got the attendance table displaying with employee names and time inputs, but you're stuck on submitting those values to your MySQL database. Let's walk through the fixes step by step—this is totally solvable!
1. Add a Form & Name Your Inputs Properly
Your current inputs don't have name attributes, so the server can't identify which time belongs to which employee. We'll wrap the table in a <form> tag and give each input a named array tied to the employee's unique ID (I assume your employés table has an id column—if not, use a unique identifier like NomEmp + PrénomEmp, but an ID is cleaner).
2. Handle Form Submission with PHP
We'll add code at the top of your script to check if the form was submitted, then loop through the submitted data and insert it into a dedicated attendance table (you should create one instead of adding to employés, since each employee will have multiple attendance records over time).
3. Database Setup Recommendation
Create a pointage (attendance) table with these fields:
idINT AUTO_INCREMENT PRIMARY KEYemp_idINT (foreign key linking toemployés.id)dateDATE (to track which day the attendance is for)morning_inTIMEmorning_outTIMEevening_inTIMEevening_outTIME
Full Modified Code
<?php // Initialize database connection $connect = mysqli_connect("localhost", "root", "ntr-ktb123", "absence"); if (!$connect) { die("Connection failed: " . mysqli_connect_error()); } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Get the current date (or add a date input to let users select it) $attendance_date = date('Y-m-d'); // Loop through each employee's submitted times foreach ($_POST['morning_in'] as $emp_id => $morning_in) { $morning_out = $_POST['morning_out'][$emp_id]; $evening_in = $_POST['evening_in'][$emp_id]; $evening_out = $_POST['evening_out'][$emp_id]; // Use prepared statements to prevent SQL injection $stmt = mysqli_prepare($connect, "INSERT INTO pointage (emp_id, date, morning_in, morning_out, evening_in, evening_out) VALUES (?, ?, ?, ?, ?, ?)"); mysqli_stmt_bind_param($stmt, "isssss", $emp_id, $attendance_date, $morning_in, $morning_out, $evening_in, $evening_out); if (mysqli_stmt_execute($stmt)) { echo "Attendance recorded for employee ID $emp_id<br>"; } else { echo "Error for employee ID $emp_id: " . mysqli_error($connect) . "<br>"; } mysqli_stmt_close($stmt); } } // Fetch employees $sql = "SELECT * FROM employés;"; $result = mysqli_query($connect, $sql); ?> <form method="POST" action=""> <table> <tr> <th>nom</th> <th>prénom</th> <th>Entrée matin</th> <th>Sortie matin</th> <th>Entrée soir</th> <th>Sortie soir</th> </tr> <?php while($row = mysqli_fetch_array($result)): ?> <tr> <td><?php echo $row['NomEmp']; ?></td> <td><?php echo $row['PrénomEmp']; ?></td> <td><input type="time" name="morning_in[<?php echo $row['id']; ?>]"></td> <td><input type="time" name="morning_out[<?php echo $row['id']; ?>]"></td> <td><input type="time" name="evening_in[<?php echo $row['id']; ?>]"></td> <td><input type="time" name="evening_out[<?php echo $row['id']; ?>]"></td> </tr> <?php endwhile; ?> </table> <button type="submit">Enregistrer les pointages</button> </form> <?php mysqli_close($connect); ?>
Key Notes:
- Input Names: Using
name="morning_in[<?php echo $row['id']; ?>]"creates an associative array where the key is the employee ID, making it easy to map each time entry to the correct person. - Time Format: The
<input type="time">returns values inHH:MM:SSformat, which is perfectly compatible with MySQL'sTIMEdata type—stick withTIMEinstead ofVARCHARfor proper date/time sorting and calculations. - SQL Injection: Always use prepared statements like in the code above to protect your database from malicious input.
- Date Tracking: I added a
datefield to record which day the attendance is for—you can modify this to let users select a date with an<input type="date">if needed.
If you don't have an id column in your employés table, replace $row['id'] with a unique combination (like $row['NomEmp'] . '-' . $row['PrénomEmp']) and adjust the emp_id field in pointage to a VARCHAR instead of INT.
内容的提问来源于stack exchange,提问作者mery

