2017-04-20 41 views
0

我有一種形式,讓我們說賬單形式。我有3張桌子。如何在同一時間使用多個表插入查詢?

  1. 法案(billId->主鍵)
  2. billdetails(billdetailId->主鍵,billId-> foregin鍵)
  3. fintran(finalId - >主鍵,總共有在形式10級的輸入。

在提交前5個輸入票據表格和其他5個輸入應該應該在法案的細節。一切都將在決賽桌去。我用下面的查詢這一點。

BEGIN; 
$sql = mysqli_query($conn,"INSERT INTO `bill`(`societyId`, `unitId`, `memberId`, `area`, `arrear`, `invoiceNumber`, `invoiceDate`, `dueDate`, `billingPeriod`, `total`, `interestOnArrear`, `totalDue`, `email`, `penalty`, `principalArrears`, `interestArrears`, `advancedAdjusted`, `netPending`) VALUES ('".$societyId."','".$unitId."','".$memberId."','".$area."','".$arrear."','".$invoiceNumber."','".$invoiceDate."','".$dueDate."','".$billingPeriod."','".$total."','".$interestOnArrear."','".$totalDue."','".$email."','".$penalty."','".$principalArrears."','".$interestArrears."','".$advancedAdjusted."','".$netPending."')");  
$memo = $_REQUEST['memo']; 
$charge = $_REQUEST['charge']; 
$sql= mysqli_query($conn,"INSERT INTO `billdetail`(`biilId`, `memo`, `charge`) VALUES (LAST_INSERT_ID(),'".$memo."','".$charge."')"); 
$sql= mysqli_query($conn,"INSERT INTO `fintran`(`societyId`, `docId`, `docTypeName`, `docDate`, `unitId`, `unitName`, `memberId`, `memberName`, `comboId`, `ledgerId`, `ledgerName`, `debit`, `credit`, `netValue`) VALUES ('".$societyId."',LAST_INSERT_ID(),'bill','','".$unitId."','".$unitId."','".$memberId."','".$memberId."','','".$charge."','','','','')"); 
COMMIT; 

插入數據後我想要billId即bill表的主鍵在billdetailsfintran表中。但有了這個查詢,我只能在billdetail表中得到這個。在fintran表中我得到了billdetail表的主鍵。請幫助我一樣。

+1

您的代碼很容易受到[** SQL注入攻擊**](https://en.wikipedia.org/wiki/SQL_injection)。你應該使用[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)或[** PDO **](https://secure.php.net/ manual/en/pdo.prepared-statements.php)準備帶有綁定參數的語句,如[**這篇文章**]所述(https://stackoverflow.com/questions/60174/how-can-i-prevent-sql步噴射功能於PHP)。 –

+0

您也可以使用'mysqli_insert_id()'將其綁定到一個變量,以便多次使用它。 – JustOnUnderMillions

+0

http://stackoverflow.com/questions/5178697/mysql-insert-into-multiple-tables-database-normalization在具有參數的過程中使用事務。 http://stackoverflow.com/questions/3053593/mysql-insert-into-2-tables告訴你如何獲取插入記錄的ID。 – xQbert

回答

相關問題