简体   繁体   English

PHP和MySQL从未知ID填充表

[英]PHP and MySQL to populate table from unknown ID

I have three tables: unitpost, file, and postfile with postfile being the table that links files to a particular post. 我有三个表:unitpost,file和postfile,其中postfile是将文件链接到特定帖子的表。

I have created a script that uploads the files and posts however I need to populate the postfile table with both the unitpostID and fileID, however once the file is uploaded, the ID is chosen at random. 我创建了一个脚本来上传文件和帖子,但是我需要同时用unitpostID和fileID填充postfile表,但是一旦文件上传,就可以随机选择ID。 How can I then populate the postfile table with the fileID that was just uploaded? 然后,如何使用刚刚上载的fileID填充后文件表?

Hope this made sense!! 希望这有道理!!

Any help appreciated. 任何帮助表示赞赏。

使用LAST_INSERT_ID()获取上一次插入操作的ID。

Use AUTOINCREMENT for the primary keys, thus the IDs won't be random, and you will be able to use them either from MySQL or from PHP. 对主键使用AUTOINCREMENT ,因此ID不会是随机的,您将可以从MySQL或PHP使用它们。

Afterwards use mysqli_insert_id() (or PDO::lastInsertId if you use PDO) from php to get the last insert id, or, as @Nelson stated, the MySQL function directly. 然后,从php使用mysqli_insert_id() (如果使用PDO,则使用PDO::lastInsertId )来获取最后的插入ID,或者直接使用MySQL函数(如@Nelson所述)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM