Php 重复表单-添加到数据库

Php 重复表单-添加到数据库,php,html,mysql,forms,duplicates,Php,Html,Mysql,Forms,Duplicates,好的,表单从一个下拉列表开始,询问“选择您有多少个孩子”,您可以选择1-10,这将生成相应数量的重复表单(完全相同的字段)。我想知道的是如何将每个重复表单添加到一个表中 例如,我想要的是:一个人有5个孩子,每个孩子会在children表中得到一行 此时,如果选择了1个子项并输入了详细信息,则可以很好地将其输入表中。那么,为什么它停止了,当它做了一个以上,我如何才能修复它 如果有人想看的话,我可以将问题更新为post代码,但是代码只是整个页面的一小部分(使用JavaScript、PHP、HTML和

好的,表单从一个下拉列表开始,询问“选择您有多少个孩子”,您可以选择1-10,这将生成相应数量的重复表单(完全相同的字段)。我想知道的是如何将每个重复表单添加到一个表中

例如,我想要的是:一个人有5个孩子,每个孩子会在children表中得到一行

此时,如果选择了1个子项并输入了详细信息,则可以很好地将其输入表中。那么,为什么它停止了,当它做了一个以上,我如何才能修复它


如果有人想看的话,我可以将问题更新为post代码,但是代码只是整个页面的一小部分(使用JavaScript、PHP、HTML和MySQL),事情会变得混乱。我只是在寻找想法和指针。

在表单中使用计数器,并在字段名中添加一个数字

<form>
<?php $counter = 1; ?>
<input type="text" name="child[<?php echo $counter; ?>][name]">
<input type="text" name="child[<?php echo $counter; ?>][age]">
... rest of the form ...
<?php $counter++; ?>
</form>


确保重复的表单在名称中使用数组

例如:

<select name="numberofchildren">
<option>1</option>
...
</select>

// generated by JS or serverside (don;t know what you do now)
Name of 1st Child: <input type="text" name="children[1][name]"/>
Age of 1st Child: <input type="text" name="children[1][age]"/>
Name of 2st Child: <input type="text" name="children[2][name]"/>
Age of 2st Child: <input type="text" name="children[2][age]"/>
Name of 3st Child: <input type="text" name="children[3][name]"/>
Age of 3st Child: <input type="text" name="children[3][age]"/>

对于正在生成的输入,您可以使用“children[]”作为名称,以便通过POST发送表单时将其填写到数组中:

`Array(
    [children] => Array(
        [0] => 'test'
        [1] => 'test2'
    )
)`

但是,给我看一下您的代码,我们会进行调整。

当您添加时,您是否会为输入分别命名,即child[1][name]、child[1][age]、child[2][name]、child[2][age]?
`Array(
    [children] => Array(
        [0] => 'test'
        [1] => 'test2'
    )
)`