如何在第一張表中插入一行,並在一個帖子中插入第二張表中的多行?如何將數據插入到一個帖子中的兩個表中?
我已經拖表中的第一個是主表和第二個是子表,例如:
1表 - >>發票 第2表 - >>sub_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>
");
}
}
入住http://stackoverflow.com/questions/5178697/mysql-insert-into-multiple-tables-database-normalization – inhan