You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将HTML input type=time的内容插入MySQL数据库?

How to Submit Time Inputs to MySQL from Employee Attendance Table

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:

  • id INT AUTO_INCREMENT PRIMARY KEY
  • emp_id INT (foreign key linking to employés.id)
  • date DATE (to track which day the attendance is for)
  • morning_in TIME
  • morning_out TIME
  • evening_in TIME
  • evening_out TIME

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 in HH:MM:SS format, which is perfectly compatible with MySQL's TIME data type—stick with TIME instead of VARCHAR for 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 date field 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:22:26