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

发布于 2024-11-06 15:41:32 字数 1870 浏览 2 评论 0原文

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

结构

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡の花香 2024-11-13 15:41:32

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

<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 数组中,每个父级都应该有一个子级记录数组。

Maybe you can change the names for the form inputs in this way:

<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> 

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.

ぺ禁宫浮华殁 2024-11-13 15:41:32

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

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]) . "')";

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:

parent1
  child1a
  child1b
parent2
  child2a

This will generate the following $_POST array:

$_POST['parent-record'] = array('parent1', 'parent2');
$_POST['child-record'] = array('child1a', 'child1b', 'child2a');

So, you can see that, after you insert parent1 into the database, you then insert all the child records, which incorrectly assigns parent1 as child2a'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!!

$sql = "INSERT INTO records (input) VALUES ('" . mysql_real_escape_string($_POST['parent-record'][$i]) . "')";
时光瘦了 2024-11-13 15:41:32

看看你的代码的最后一部分:

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++

Take a look at the last part of your code:

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);
    }

I think it should be:

 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++ instead of i++

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文