dongmangzong8006 2016-11-15 16:38
浏览 28

在提交期间抓取记录ID并保存到另一个表

I have two tables:

Invoices & Invoices Parts

The reason i'm using two tables is because i dont want to save the invoice parts array into one record on the Invoices table and i dont want to save it into multiple records on the Invoices table.

When i submit the data into both tables, how can i link both tables together with the ID if i dont have the ID yet?

$result = mysqli_query($mysqli, "INSERT INTO invoices(labelwarning, status, todaysdate, todaystime, todaysdatetime, avatar, firstname, lastname, mwaid, nextgenid, manager, manageremail, dispatcher, dispatcheremail, techemail, tag, serialnumber, currentequipment, company, address, city, state, zip, contactperson, contactnumber , currentdate, timearrived, timecompleted, login_id) VALUES('$labelwarning', '$status', '$todaysdate', '$todaystime', '$todaysdatetime',  '$avatar', '$firstname', '$lastname', '$mwaid', '$nextgenid', '$manager', '$manageremail', '$dispatcher', '$dispatcheremail', '$techemail', '$tag', '$serialnumber', '$currentequipment', '$company', '$address', '$city', '$state', '$zip', '$contactperson', '$contactnumber ', '$currentdate', '$timearrived', '$loginId')");

$result2 = mysqli_query($mysqli, "INSERT INTO invoiceparts(partnumber, partdescription, partprice, partquantity, partdb, login_id) VALUES '$partnumberstring', '$partdescriptionstring', '$partpricestring', '$partquantitystring', '$partdbstring', '$loginId')");
  • 写回答

1条回答 默认 最新

  • duanpaxin3531 2016-11-15 17:13
    关注

    If you need to link the "login_id" and the login_id is generated with AUTO_INCREMENT then you can do it by

    • for Mysqli connection, mysqli_insert_id($mysqli);
    • for PDO connection, $mysqli->lastInsertId();

    If the login_id is not an AUTO_INCREMENT field then you can do it by

    • SELECT login_id FROM invoices ORDER BY login_id DESC LIMIT 1;

    It'll return the last inserted row from there we can fetch the login_id

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器