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 8025053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:02:32+00:00 2026-06-04T23:02:32+00:00

Let there be a PHP method which is supposed to insert a few rows

  • 0

Let there be a PHP method which is supposed to insert a few rows into a MySQL database, each time it’s called. Unfortunately these records represent a hierarchy and I want to persist this hierarchy by adding a key for each level. An example could be looking like this:

id    hier_key_1    hier_key_2    value
0     0             0             val1
1     0             0             val2
2     1             0             val3
3     1             0             val4
4     2             1             val5
5     2             1             val6

My current solution works like this:

  1. Insert new record in hier_key_1 table
  2. Select auto-incremented id of this record
  3. Insert new record in hier_key_2 table
  4. Select auto-incremented id of this record
  5. Insert record in table with all values and hier_key_1 and hier_key_2 as foreign keys pointing to their tables

Is there a way to simplify this, i.e. does the insert method provides any kind of return value including the keys of the inserted data? Or should I try to generate unique keys in my PHP method and check for duplicates afterwards?

  • 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-06-04T23:02:34+00:00Added an answer on June 4, 2026 at 11:02 pm

    Yes, there are:

    PHP:

    $query = "INSERT INTO hier_key_1(hier_key_1) VALUES (0)"); 
    if( !mysql_query($query) ) 
    {/*insert failed*/} 
    
    $id_key_1 = mysql_insert_id();
    
    //... and so on... 
    

    MYSQL – SQL

    INSERT INTO hier_key_1(hier_key_1) VALUES (0); SET @id_key_1 =
    LAST_INSERT_ID();
    
    INSERT INTO hier_key_2(hier_key_2) VALUES (0); SET @id_key_2 =
    LAST_INSERT_ID();
    
    INSERT INTO table_name(id, hier_key_1, hier_key_1,value) VALUES (0,
    @id_key_1,@id_key_2,value1);
    

    and so on..

    greetings

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a query to run in php that would let you insert all
Is there any reflection/introspection/magic in PHP that will let you find the PHP file
Let's say there is a file called myfile.txt with the following contents: one two
(PHP/MySQL) Let me preface by saying that this works, and that I'm wondering if
I have a file called init.php which I want to get automatically included in
Let's say there is a page with 100 different user photo's shown on the
Let's say there's a table created as follows: create table testTable ( colA int
Let's say there are two python scripts that want to write data to the
Let's say there's following directory structure: root | +--projects | | | +-test |
Let's say there's the following directory structure: projects | +--lib1 | | | +-CMakeFiles.txt

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.