tags:

views:

109

answers:

2

Hi, I have 4 Tables

[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]
UserID (fk) | project_id (fk) | project_category_id (fk)
    2              4                  1
A: 

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?

sandeepan
A: 

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.

Steve Claridge
no, I want the same with the [project_select] table, how can I insert value in the tables, to get the value automatically in the FK field?
elmanino
Not sure I follow you. Using mysql_insert_id to retrieve the newly created primary keys on your three tables will give you the keys to insert into project_select.
Steve Claridge
updated my question
elmanino
It should work for what you want but there's no way to insert into three table, store the PKs and then insert them into another table all in one query.
Steve Claridge
thank you, what's the best method to insert my Data in all 4 tables? with mysql_insert_id? can you show me an example pleas?
elmanino
I found a very good example on this page http://www.desilva.biz/mysql/insertid.html thank you again!
elmanino