foreign key values

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?

$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.”);

table: members
| members_id | username | firstname | lastname | password | date | ip |

table: blogs
| blog_id | members_id | title | entry | blog_date | date | ip |

and you queries would look similar to this:

<?php // the following examples have the member logged in and using session 'members_id' // Select $query = "SELECT * FROM members, blogs WHERE members.members_id=blogs.members_id AND members.members_id={$_SESSION['member_id']}"; // Insert $query = "INSERT INTO blogs (members_id, title, entry, blog_date, date, ip) VALUES ('{$_SESSION['member_id']}', '$title', '$entry', '$date', NOW(), '$ip')"; // Delete $query = "DELETE FROM blogs, members WHERE members.members_id=blogs.members_id AND members.members_id={$_SESSION['member_id']}"; ?>


Hope this helps


Sponsor our Newsletter | Privacy Policy | Terms of Service