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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:05:26+00:00 2026-06-04T08:05:26+00:00

How to insert one row in the first table and many rows in the

  • 0

How to insert one row in the first table and many rows in the 2nd table in one post?

I’ve tow tables the first one is the main table and the 2nd is the sub-table, for example:

1st table –>> invoices
2nd table –>> sub_invoices

each invoice is linked to one or many sub-invoices.

so, I need to insert the data from one post into the tow tables, one row into invoices and one or many into sub_invoices.

invoices ::
invoice_id, date, total, drvr_name, mobile, car_no, to, from, username

sub_invoices ::
invoice_id, contract_no, inv_sub_no, dest, contract_vlu, advncd_sum, late_sum, discount_extra, rcvd_sum, notes, job_no

if(isset($_POST['addinvoice2']) and $_POST['addinvoice2'] == 'addinvoice2'){
            //invoices::
            $drvr_name = $_POST['drvr_name'];
            $mobile = $_POST['mobile'];
            $car_no = $_POST['car_no'];
            $to = $_POST['to'];
            //sub_invoices::
             foreach($_POST['contract_no'] as $index => $contractno) {

                    $contract_no = $contractno;
                    $inv_sub_no = $_POST['inv_sub_no'][$index];
                    $dest = $_POST['dest'][$index];
                    $contract_vlu = $_POST['contract_vlu'][$index];
                    $advncd_sum = $_POST['advncd_sum'][$index];
                    $late_sum = $_POST['late_sum'][$index];
                    $discount_extra = $_POST['discount_extra'][$index];
                    $rcvd_sum = $_POST['rcvd_sum'][$index];
                    $notes = $_POST['notes'][$index];
                    $job_no = $_POST['job_no'][$index];
             } 


                $date=date('d-m-y h:iA');

                $MAIN = mysql_query("INSERT INTO `invoices`(`invoice_id`, `date`, `total`, `drvr_name`, `mobile`, `car_no`, `to`, `from`, `username`)
                 VALUES
                 ('','$date','','$drvr_name','$mobile','$car_no','$to','','') 
                ") or die(mysql_error());



                $last_id_in_invoices = mysql_insert_id();

                $SUB = "INSERT INTO `sub_invoices`
                (`invoice_id`, `contract_no`, `inv_sub_no`, `dest`, `contract_vlu`, `advncd_sum`, `late_sum`,
                 `discount_extra`, `rcvd_sum`, `notes`, `job_no`) 
                VALUES ($last_id_in_invoices,$contract_no,$inv_sub_no,$dest,$contract_vlu,$advncd_sum,$late_sum,$discount_extra,$rcvd_sum,$notes,$job_no)";



                $RESULT_SUB = mysql_query($SUB);

                if (isset($MAIN, $RESULT_SUB)){
                    die ("
                        <center>Done Successfully!</center>
                        ");
                }
            }
  • 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-04T08:05:29+00:00Added an answer on June 4, 2026 at 8:05 am

    Pretty sure you’ll to use two insert statements, why wouldn’t you do something like this?

    INSERT INTO INVOICES....  and INSERT INTO SUB_INVOICES...(perhaps in a loop)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MySQL. I need to insert one row into a table first,
I have this test table with one row entry and 2 indexes, the first
I am attempting to insert a copy of a row from one table into
I have one multi-row INSERT statement (300 or so sets of values) that I
I'm trying to insert multiple rows into MySql with only one INSERT INTO statement
I am trying to insert data from one table that was imported from an
What is the best way to insert rows into tables with references 1 to
I have many tables in my database which are interrelated. I have a table
In my SQLite database only one row effect but I want effect 4 rows
I have a MySQL table that will only have one row. What should my

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.