开发者

Adding data to a table from the columns of auto-incremented primary keys of other tables

I have three tables: members, blobs, and contactlist. The members table is already populated with a primary key 'id' (which is an auto-increment column). The blob table is already populated with a primary key 'post_id' (which is also an auto-increment column).

So in the contactlist table, I have 3 empty columns: 'mem_id' (foreign key to 'id'), 'tag_id' (foreign key to 'post_id') and 'contacts'.

And I want to insert:

  1. the user id ('id') from the members table into the 'mem_id' column
  2. the blob id ('post_id') from the blob table into the 'tag_id' column
  3. the list/array of contacts that are entered from an html form into the 'contacts' column

But since the 'id' and 'post_id' are INT's based on auto-increments from tables, I'm not sure how to include these in the INSERT statement?

Here's what I have:

foreach($contacts as $contact) {
  $sql = mysql_query("INSERT INTO contactlist 
                       (mem_id, post_id, contacts) 
                      VALUES
                       ('not sure what to put here!','$contact')") or die开发者_JS百科 (mysql_error());
}


Let's suppose you have retrieve the mem_id and the post_id int php variables, you need to do the following:

$sql = mysql_query("INSERT INTO contactlist 
                   (mem_id, post_id, contacts) 
                VALUES
                   (".$mem_id.",".$post_id.",'".$contact."')") 
           or die (mysql_error());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜