php - 如何同时对多个表使用插入查询?

标签 php mysql insert last-insert-id

我有一张表格,比如说账单表格。我有 3 张 table 。

  1. 账单(billId->主键)
  2. billdetails(billdetailId->主键,billId->外键)
  3. 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,即billdetailsfintran表中账单表的主键。但通过此查询,我只能在 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/

相关文章:

php - 选定的自动完成值未显示在输入框中

php - Imagecreatefromwebp() : WebP decode: fail to decode input data

mysql - 虚拟环境中没有名为 'mysql' 的模块

linux - 如何找到属于内核的地址

javascript - 表单不是在 chrome 上提交而是在 firefox 上提交

javascript - 表单验证检查后不显示模态框

php - 拉维尔 : same table relationship error

php - CActiveDataProvider 根本不返回任何数据,只返回结构

mysql - 为什么 ENUM 不在 MySQL 中存储多个值?

c - 准备好的语句中的 SQLite 绑定(bind)函数