2016-11-15 51 views
0

我有兩個表:抓鬥記錄ID期間提交併保存到另一臺

發票&發票零部件

我使用兩個表的原因是因爲我不想保存發票零部件陣列到Invoices表中的一個記錄中,我不想將它保存到Invoices表中的多個記錄中。

當我將數據提交到兩個表中時,如何將兩個表與ID連接在一起,如果我沒有ID?

$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

你試過http://php.net/manual/en/mysqli.insert-id.php? –

回答

1

如果需要鏈接 「LOGIN_ID」 和LOGIN_ID與AUTO_INCREMENT產生的,那麼你可以通過

  • 爲mysqli的連接,mysqli_insert_id($ mysqli的)去做;
  • 用於PDO連接,$ mysqli-> lastInsertId();

如果LOGIN_ID不是AUTO_INCREMENT字段,那麼你可以通過

  • SELECT LOGIN_ID FROM發票ORDER BY LOGIN_ID DESC LIMIT 1這樣做;

它會返回最後插入的行從這裏我們可以獲取LOGIN_ID

相關問題