Jump to content

Implement foriegn key


mdvignesh

Recommended Posts

Foreign keys are a type of constraint (or condition) on a column and the foreign key condition is that the column data 'must be equal to a value in the other table's column.'  So, you insert a value that already exists in the other (foreign) table and just insert it like any other value.  The foreign table does need to be populated with values first before you can start adding to tables that reference the data.

 

Hopefully that makes sense.

 

This probably should have been put in a SQL forum.

Link to comment
Share on other sites

If you for example have the user_id from the users table as a foreign key in a different table, then you need to get that key somehow, there are different ways to do it. You could run a SELECT query and get the user_id and store it into a variable and then INSERT the same variable into the next INSERT query like this INSERT INTO table (contribution, user_id) VALUES ('$con', '$user_id');

 

You could also store the user_id into a SESSION variable after log in and then access the user_id from the session variable from then on.

 

I'm sure there are other ways, I hope that helped, I'm a beginner myself.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.