我有一张表格,比如说账单表格。我有 3 张 table 。
- 账单(billId->主键)
- billdetails(billdetailId->主键,billId->外键)
- fintran(finalId -> 主键,表单中共有 10 个输入。
提交时,前 5 个输入应进入账单表,其他 5 个输入应进入账单详细信息。一切都将进入决赛 table 。我为此使用了以下查询。
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
,即billdetails
和fintran
表中账单表的主键。但通过此查询,我只能在 billdetail
表中获取此信息。在 fintran 表中,我获取了 billdetail 表的主键。请帮助我。
最佳答案
No, you can't insert into multiple tables in one MySQL command. You can however use transactions.
检查这个:MySQL Insert into multiple tables? (Database normalization?)
关于php - 如何同时对多个表使用插入查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43523968/