doukekui0914 2015-05-25 05:08
浏览 52
已采纳

将动态PHP表单插入MySQL

I have a form which is dynamic and user can add more rows to insert different hours, I wonder which is the best way to add those data to MySQL database.

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

Inputs are expandable so I don't know how many rows they will send to save in mysql.

  • 写回答

1条回答 默认 最新

  • dongxiao0449 2015-05-25 05:36
    关注

    you can use a foreach(), using the key to bind the input groupings together

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起