开发者

Insert Mysql with PHP - problem with foreign key insert

开发者 https://www.devze.com 2023-01-18 13:29 出处:网络
[project_select] UserID (fk) | project_id (fk) | project_category_id (fk) [project_category] project_category_id | category
[project_select]
UserID (fk) | project_id (fk) | project_category_id (fk)

[project_category]
project_category_id | category

[projects]
project_id | projectname

[project_user]
UserID | Name

How can I insert Data with php in the tables project_category, projects and project_user, to get automatically the values in the project_select table with the FK's?

Update: How can I merge this 3 queries into one line?

INSERT INTO project_category
VALUES (1,'Fruits')

INSERT INTO projects
VALUES (4,'Apple')

INSERT INTO project_user
VALUES (2,'Adam')

and get this values with this one query in the project_select table:

[project_select开发者_如何学Python]
UserID (fk) | project_id (fk) | project_category_id (fk)
    2              4                  1


You can use the mysql_insert_id function to retrieve the id you just inserted.

mysql_query("INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')");
$person_id = mysql_insert_id();

So, do inserts on your tables, call mysql_insert_id() after each insert, store inserted IDs and finally use the IDs in the mysql command to create the join table.


I doubt that I understood your problem correctly. But if you want to have the values entered automatically in project_select table? Then how about using MySql triggers? But be sure you are well aware of the database internals before using it.

What are triggers?, Setting up triggers

But, please describe this -

problem with foreign key insert?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号