2
[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
1
  • @elmanio what exactly do you mean by "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?". I guess many others did not understand completely. Commented Oct 4, 2010 at 20:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
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.
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?
I found a very good example on this page desilva.biz/mysql/insertid.html thank you again!
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.