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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:03:53+00:00 2026-06-04T02:03:53+00:00

I have two tables in MySql database, one is sales_order and the other is

  • 0

I have two tables in MySql database, one is sales_order and the other is sales_order_details which contains the details of the order in the sales_order table. Obviously, there is a one to many relationship from sales_order to sales_order_details.

When a customer places an order, the first entry is made into the sales_order table and based on the auto_increment id of the sales_order table, the corresponding entry is made into the sales_order_details table.

I’m using the last_insert_id() MySql function to retrieve the corresponding order_id from the sales_order table, Something like this.

insert into sales_order_details(order_id, prod_id, prod_price)values(last_insert_id(), 5, 1500);

It’s working. The last_insert_id() function retrieves the last inserted id from the respective table which is unique to a particular connection (as far as I know).

Now, I need the same order_id which is retrieved and inserted most recently by the last_insert_id() function to send it as an invoice number to a payment system.

I can try using the PHP function mysql_insert_id() but I’m not sure whether it works as specified. What is the correct way to retrieve the last insert id which always guarantees to retrieve the specific id which is always associated with a particular order?

  • 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-04T02:03:55+00:00Added an answer on June 4, 2026 at 2:03 am

    There are 2 ways which you could solve this:

    After inserting the entry in the sales_order:

    1. Get the last_insert_id and store it in a php variable, then inject this value into the relevant queries.

    2. Store the last_insert_id in a MySql user-defined variable and use the variable in the query instead of last_insert_id.

    sample code for option 2

    SET @last_order_id = last_insert_id();

    insert into sales_order_details (order_id, prod_id, prod_price)
    values (@last_order_id, 5, 1500);`
    
    insert into sales_invoice (order_id, invoice_id)
    values (@last_order_id, 1);`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I have is two tables inside of a mysql database. One table contains
In a MySQL database I have two tables linked in a join. One table
I have a database that has two tables, one of which contains a foreign
I have two tables in my MySQL database, one is a library of all
I have the following two tables in my mysql database. Table Name: groups id
I have two MySQL database tables: one containing a list of championships and another
I am using Mysql. And I have two tables which are one-to-one related with
I have two tables in a MySql database: COUNTRY --------------- id, country_name and CITY
I am working on PHP and database MySQL. I have two tables in SQL
I have 10 tables in my database(MySQL). two of them is given below tbl_state

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.