我有一个数据库,由 2 个具有“一对多”关系的表组成
the first table:
CREATE TABLE data (
personid int not null auto_increment,
name varchar(255),
phone int ,
primary key (personid)
)
第二个表:
CREATE TABLE links (
linkid int not null auto_increment,
link varchar(255),
personid int,
primary key (linkid),
foreign key (personid)
references data(personid)
)
我向第一个表插入记录,每条新记录都会获得一个新ID,然后输入指向该ID的链接,我想知道如何知道通过php/sql插入链接给他的人的ID
最佳答案
使用Mysqli
或PDO
-
使用 PDO:
$personid = $pdo->lastInsertId();
使用 Mysqli:
$personid = $mysqli->insert_id;
然后再次使用上面的$personid
插入数据links
表数据。
您必须开始使用 Mysqli
或 PDO
才能在 PHP
中编写代码。
遵守标准:)
关于php - 如何知道 "one to many"数据库中自增列的ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46461112/