Php 嵌套输入字段(父结构和子结构)

Php 嵌套输入字段(父结构和子结构),php,webforms,parent-child,Php,Webforms,Parent Child,我处于一个非常棘手的情况下,我有一个允许输入记录的页面,并且有一个父/子结构(或者记录/子记录,如果你调用它)。当用户需要添加更多记录时,可以动态添加许多记录/子记录(行被克隆并插入到下一行之后) 结构: <div class="row"> <strong>Parent record:</strong> <input type="text" name="parent-record[]" /> <div class="row">

我处于一个非常棘手的情况下,我有一个允许输入记录的页面,并且有一个父/子结构(或者记录/子记录,如果你调用它)。当用户需要添加更多记录时,可以动态添加许多记录/子记录(行被克隆并插入到下一行之后)

结构

<div class="row">
   <strong>Parent record:</strong> <input type="text" name="parent-record[]" />
   <div class="row">
       <strong>Child record:</strong> <input type="text" name="child-record[]" />
   </div>
   <span class="add-subrecord">Add another sub-record</span>
</div>
<span class="add-record">Add another record</span> 

父记录:
子记录:
添加另一个子记录
添加另一条记录
PHP方面的问题是,当我有2个for循环时,1个for循环如下:

for($i = 0; $i < count($_POST['parent-record']); $i++)
{
    $sql = "INSERT INTO records (input) VALUES ('" . $_POST['parent-record'][$i] . "')";
    $result = $db->sql_query($sql);

    $parent_id = $db->sql_nextid(); // mysql_insert_id

    for($j = 0; $j < count($_POST['child-record']); $j++)
    {
        $sql = "INSERT INTO records (input, parent) VALUES('" . $_POST['child-record'][$j] . "', '" . $parent_id . "')"; // this will also insert the $parent_id into this record because this record is a child of a parent.
        $result = $db->sql_query($sql);
    }
}
for($i=0;$isql\u查询($sql);
$parent\u id=$db->sql\u nextid();//mysql\u insert\u id
对于($j=0;$jsql\u查询($sql);
}
}
但是,当流程完成时,每个子项都有第一条主记录的父项,即使我添加了2-3条主记录(不是子记录),如下所示:

+------+---------+----------+
|id |输入|父项|
+------+---------+----------+

|1 | random | 0 |查看代码的最后一部分:

for($j = 0; $j < count($_POST['child-record']); $i++)
    {
        $sql = "INSERT INTO records (input, parent) VALUES('" . $_POST['child-record'][$j] . "', '" . $parent_id . "')"; // this will also insert the $parent_id into this record because this record is a child of a parent.
        $result = $db->sql_query($sql);
    }
for($j=0;$jsql\u查询($sql);
}
我认为应该是:

 for($j = 0; $j < count($_POST['child-record']); $j++)
    {
        $sql = "INSERT INTO records (input, parent) VALUES('" . $_POST['child-record'][$j] . "', '" . $parent_id . "')"; // this will also insert the $parent_id into this record because this record is a child of a parent.
        $result = $db->sql_query($sql);
    }
for($j=0;$jsql\u查询($sql);
}

j++而不是i++

您的问题在于对记录进行迭代的方式。首先插入第一个父记录。然后,迭代所有子记录——包括属于不同父级的子记录。例如,采用以下用户输入:

parent1
  child1a
  child1b
parent2
  child2a
这将生成以下$\u POST数组:

$_POST['parent-record'] = array('parent1', 'parent2');
$_POST['child-record'] = array('child1a', 'child1b', 'child2a');
因此,您可以看到,在将
parent1
插入数据库后,您将插入所有子记录,这将错误地将
parent1
指定为
child2a
的父记录

你需要一种方法来确定哪些孩子属于哪个家长

并记住在将用户输入插入SQL查询之前对其进行编码

$sql = "INSERT INTO records (input) VALUES ('" . mysql_real_escape_string($_POST['parent-record'][$i]) . "')";

也许您可以通过以下方式更改表单输入的名称:

<div class="row">
  <strong>Parent record:</strong> <input type="text" name="parent-record[1]" />
  <div class="row">
    <strong>Child record:</strong> <input type="text" name="parent-record[1]child-record[]" />
  </div>
  <span class="add-subrecord">Add another sub-record</span>
</div>
<span class="add-record">Add another record</span> 

父记录:
子记录:
添加另一个子记录
添加另一条记录
我假设您正在使用javascript添加新的输入。您可以使用上述名称,但增加数字


在PHP post数组中,每个父项都应该有一个子记录数组。

我不完全确定如何以某种方式实现排序功能。。。我知道清洁输入-这只是一个演示code@lolwut您可以在每个组的名称中添加一个唯一的编号。例如,第一个父项将命名为“parent-record-0”。“parent-record-0”的子记录将具有名称“child-record-0-0”、“child-record-0-1”等@lolwut实际上,子记录可以是数组。因此,第一个父记录将命名为“parent-record-0”,其子记录将命名为“child-record-0[]。第二个父项将命名为“parent-record-1”,其子项记录将命名为“child-record-1[]。等
<div class="row">
  <strong>Parent record:</strong> <input type="text" name="parent-record[1]" />
  <div class="row">
    <strong>Child record:</strong> <input type="text" name="parent-record[1]child-record[]" />
  </div>
  <span class="add-subrecord">Add another sub-record</span>
</div>
<span class="add-record">Add another record</span>