我处于一个非常棘手的情况,我有一个允许输入记录的页面,并且有一个父/子结构(或记录/子记录,如果你调用它)。当用户需要添加更多记录/子记录时,可以动态添加许多记录/子记录(克隆行并将其插入到下一行之后)。
结构:
<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);
}
}
但是,当该过程完成后,每个子记录都有一个第一个主记录的父记录,即使我添加了 2-3 个主记录(不是子记录),如下所示:
+------+---------+----------+
| id | input | parent |
+------+---------+----------+
| 1 | random | 0 | <- indicates it is a parent
| 2 | random1 | 1 | <- child record, parent is record id: 1
| 3 | random2 | 1 | <- child record, parent is record id: 1
| 4 | random3 | 1 | <- this should be a parent, but it's added as a child
| 5 | random4 | 1 | <- this one too
+------+---------+----------+
我需要某种解决方案,在创建所有子级都具有记录块的父 ID 的嵌套输入表单时,这将如何工作。
也许您可以通过以下方式更改表单输入的名称:
<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 数组中,每个父级都应该有一个子记录数组。
您的问题在于您迭代记录的方式。 首先插入第一个父记录。 然后,循环访问所有子记录,包括属于不同父记录的子记录。 例如,采用以下用户输入:
parent1
child1a
child1b
parent2
child2a
这将生成以下 $_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]) . "')";
看看代码的最后一部分:
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; $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);
}
J++ 而不是 I++