drayarms Posted January 12, 2011 Share Posted January 12, 2011 If the primary key of an sql table is being used as a foreign key of another table, when entering the values of the columns of the second table in an insert query, what would the value of the foreign key be? Say for example we had a table called members whose primary key (member_id) is a foreign key column of another table, blogs. If we wrote a query to insert values into the blogs table like the one below, how do we account for the member_id column? Would the IDENT_CURRENT('members') function be appropriate in a case like this? Please help!! $query = "INSERT INTO blogs (blog_id, title, entry, blog_date) VALUES (0, '{$_POST['title']}', '{$_POST['entry']}', NOW())"; Just in case it might be helpful, here is the query for inserting values into the members table: $query = mysql_query("INSERT INTO members (id, username, firstname, lastname, password, date, ip) VALUES (0, '$_POST[username]','$_POST[firstname]','$_POST[lastname]','$_POST[password]','$date','$ip')") or die ("Error - Couldn't register user."); drayarms Forum Newbie Posts: 13 Joined: Fri Dec 31, 2010 3:11 pm Quote Link to comment Share on other sites More sharing options...
kickstart Posted January 21, 2011 Share Posted January 21, 2011 Hi You just need to know the member id, which in most circumstances you would have already got in memory as the member would be logged in. Failing that if you knew the member name and wanted the id then you could use a sub select in insert to get the member id. All the best Keith Quote Link to comment 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.