在MySQL中插入动态PHP表单


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(),使用键将输入分组绑定在一起

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)
}