未使用 mysql 在子表中填充外键。所有编程和 sql 方面的新手。创建网站表单以使用 PHP/MYSQL 填充人员数据,然后启动辅助网站表单以填充汽车维护数据。问题是父表中的外键没有更新子表。任何帮助深表感谢。
父表:
$sql = "CREATE TABLE persons
(
Firstname CHAR(15),
Lastname CHAR(15),
Age INT,
PRIMARY KEY(Firstname)
)";
子表:
$sql = "CREATE TABLE cardata
(
CID INT NOT NULL AUTO_INCREMENT,
Firstname CHAR(15) NOT NULL,
Manufacturer CHAR(25),
Model CHAR(15),
Year INT,
`Oil Miles` INT,
`Oil Date` DATE,
`Rotation Miles` INT,
`Rotation Date` DATE,
PRIMARY KEY(CID),
FOREIGN KEY (Firstname) REFERENCES persons (Firstname)
ON UPDATE CASCADE ON DELETE CASCADE
)";
父表插入:
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
子表插入:
$sql="INSERT INTO cardata (Firstname,Manufacturer)
VALUES
('LAST_INSERT_ID()','$_POST[manufacturer]')";