将动态 PHP 表格插入 MySQL

Insert Dynamic PHP form into MySQL

我有一个动态表单,用户可以添加更多行来插入不同的时间,我想知道将这些数据添加到 MySQL 数据库的最佳方式是什么。

<input style="margin-left:28px;" type="image" class="add_field_button" src="img/add.png" />
<table id="dataTable" width="900" style="margin-left:25px; margin-top:10px;">
    <tr style="text-align:center;">
        <td width="70">From</td>
        <td>To</td>
        <td>Duration Hours</td>
        <td>Code</td>
        <td>Remark</td>
    </tr> 
    <tr style="height:85px; text-align:center;">
        <td><input type="text" name="hours1[]" id="hours1" /></td>
        <td><input type="text" name="hours2[]" id="hours2" /></td>
        <td><input type="text" name="durationh[]" id="durationh" /></td>
        <td><input type="text" name="hrscode[]" id="hrscode" /></td>
        <td><textarea name="remark[]" id="remark"></textarea></td>

    </tr> 
</table>

输入是可扩展的,所以我不知道它们将发送多少行以保存在 mysql

您可以使用 foreach(),使用键将输入分组绑定在一起[=13​​=]

foreach($_POST['hours1'] as $key => $value) { // could use any of the fields in $_POST['hours1']
    //$_POST['hours1'][$key];
    //$_POST['hours2'][$key];
    //$_POST['durationh'][$key];
    //$_POST['hrscode'][$key];
    //$_POST['remark'][$key];

    //INSERT INTO table (columns) VALUES (your $_POST values)
}