douyijin7741 2012-05-19 22:04
浏览 42
已采纳

在循环中将两个表单字段一起插入[关闭]

I have two form fields "First name" and "Last name" I can also clone the two fields to add more users on one submit. I want each record to have it's own ID once inserted into the database. However I can't seen to get it to insert two values at once in a loop.

My form fields are

 <input type="text" name="firstname_0" size="35" value="" />
 <input type="text" name="lastname_0" size="35" value="" />

The name auto increments every time I want to add another user firstname_1, lastname_1 etc...

I would like to add 1 or unlimited users in one form submit.

Any help would be appreciated.

I tried using a foreach() and while but can't get the concept.

  • 写回答

4条回答 默认 最新

  • dqd82461 2012-05-19 22:07
    关注

    Use arrays in the inputs' name attribute.

    <input type="text" name="users[0]['firstname']" size="35" value="" />
    <input type="text" name="users[0]['lastname']" size="35" value="" />
    
    <input type="text" name="users[1]['firstname']" size="35" value="" />
    <input type="text" name="users[1]['lastname']" size="35" value="" />
    

    And then:

    foreach ($_POST['users'] as $user) {
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?