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>
");
}
}