Jump to content

foreign key values


drayarms

Recommended Posts

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

 

Link to comment
Share on other sites

  • 2 weeks later...

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

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.