Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4599598
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:33:50+00:00 2026-05-21T23:33:50+00:00

I am in a real tricky situation where I have a page that allows

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:33:51+00:00Added an answer on May 21, 2026 at 11:33 pm

    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]) . "')";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any real sequence of characters that always compares greater than any other
I have a really tricky task to do: there is an existing web project
I have a tricky question. As everyone knows, Apple clearly says that ONE UIViewController
I have a HTML+CSS mobile website (not a real app) that is supposed to
I've recently decided that I just have to finally learn C/C++, and there is
I have a SharePoint list that is populated via an InfoPath form. There are
The real question: Is there a way to clear certain attributes for all components
Say I have real, dimension(0:100) :: realResults and I want to iterate over realResults
Is there any real difference to screen reader users (or any users really) whether
I have 2 simple, but maybe tricky questions. Let´s say I have assembler instruction:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.