Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Thanked 0 Times in 0 Posts

    any way to insert data into two tables at the same time?

    what I have is a table called `order` and a table called `lmcharge`.
    a person inputs data from a form and i need to take the data from both and some goes into one table, some goes into another
    here is an example:
    order values:
    student_id, class_id, order_type, date, status
    //student_id, class_id, order_type will be from the form

    lmcharge values:
    student_id, order_id, lm_number
    //student_id, and lm_number will also be from the form but order_id will have to be from the order table

    anyone have a clue?

  2. #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    St. Catharines, Ontario Canada
    Thanked 156 Times in 147 Posts
    make the order_id in the orders table an auto increment column and then use last_insert_id() to retrieve the value to insert into the second table. You would do two inserts and should wrap them in a transaction.

    See the link in my signature to view an article on using last_insert_id.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts