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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献