0

如何在一个帖子中在第一个表中插入一行,在第二个表中插入多行?

我有两个表,第一个是主表,第二个是子表,例如:

第一张表 -->>发票 第二张表 -->> sub_invoices

每张发票都链接到一张或多张子发票。

所以,我需要将一个帖子中的数据插入到拖车表中,将一行插入到invoices中,将一个或多个插入到sub_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>
                        ");
                }
            }
4

1 回答 1

0

很确定你会使用两个插入语句,你为什么不做这样的事情呢?

INSERT INTO INVOICES....  and INSERT INTO SUB_INVOICES...(perhaps in a loop)
于 2012-05-15T18:04:47.733 回答