mdvignesh Posted February 20, 2011 Share Posted February 20, 2011 I have created foreign key column. But I don't how to use it when inserting or updating Can anyone give examples? Quote Link to comment https://forums.phpfreaks.com/topic/228285-implement-foriegn-key/ Share on other sites More sharing options...
thehippy Posted February 20, 2011 Share Posted February 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228285-implement-foriegn-key/#findComment-1177275 Share on other sites More sharing options...
chaseman Posted February 20, 2011 Share Posted February 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228285-implement-foriegn-key/#findComment-1177314 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.