嵌套输入字段(父子结构)
我处于一个非常棘手的情况,我有一个允许输入记录的页面,并且有一个父/子结构(或记录/子记录,如果你称之为)。当用户需要添加更多记录/子记录时,可以动态添加许多记录/子记录(行被克隆并插入到下一行之后)。
结构:
<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。
I am in a real tricky situation where I have a page that allows records to be inputted and there is a parent/child structure (or records/sub-records if you call it). Many records/sub-records can be added dynamically (the rows are cloned and inserted after the next row) when the user needs to add more.
The structure:
<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>
The problem on the PHP side, when I have 2 for loops, 1 in a for loop like this:
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);
}
}
However, when the process has done, each child has a parent of the first main record, even when I add 2-3 main records (not child records) so like:
+------+---------+----------+
| 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
+------+---------+----------+
I need some kind of solution how this will work when creating nested input form that all child have a parent id of the record block.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您可以通过以下方式更改表单输入的名称:
我假设您正在使用 javascript 添加新输入。您可以使用上面的名称,但增加数字。
在 PHP post 数组中,每个父级都应该有一个子级记录数组。
Maybe you can change the names for the form inputs in this way:
I'm assuming you're using javascript to add new inputs. You can use the names as above but increment the number.
In the PHP post array each parent should have an array of child records.
您的问题在于您迭代记录的方式。首先插入第一条父记录。然后,您迭代所有子记录——包括属于不同父记录的子记录。例如,采用以下用户输入:
这将生成以下 $_POST 数组:
因此,您可以看到,在将
parent1
插入数据库后,您随后插入了所有子记录,这是错误的将parent1
指定为child2a
的父级。您需要一种方法来确定哪些孩子属于哪个父母。
并记住在将用户输入插入 SQL 查询之前对其进行编码!
Your problem lies in the way in which you are iterating over the records. You start by inserting the first parent record. Then, you iterate over all child records--including the child records that belong to different parents. For example, take the following user input:
This will generate the following $_POST array:
So, you can see that, after you insert
parent1
into the database, you then insert all the child records, which incorrectly assignsparent1
aschild2a
's parent.You need a way of determining which children belong to what parent.
AND REMEMBER to encode your user input before inserting it into a SQL query!!
看看你的代码的最后一部分:
我认为应该是:
j++而不是i++
Take a look at the last part of your code:
I think it should be:
j++ instead of i++