【问题标题】:Adding data to a table from the columns of auto-incremented primary keys of other tables从其他表的自增主键的列向表中添加数据
【发布时间】:2011-09-02 21:26:57
【问题描述】:

我有三个表:成员、blob 和联系人列表。成员表已经填充了主键“id”(这是一个自动增量列)。 Blob 表已经填充了主键“post_id”(这也是一个自动增量列)。

所以在联系人列表中,我有 3 个空列:“mem_id”(“id”的外键)、“tag_id”(“post_id”的外键)和“联系人”。

我想插入:

  1. 成员表中的用户 id ('id') 到 'mem_id' 列中
  2. blob 表中的 blob id ('post_id') 到 'tag_id' 列中
  3. 从 html 表单输入到“联系人”列的联系人列表/数组

但由于 'id' 和 'post_id' 是基于表的自动增量的 INT,我不确定如何将它们包含在 INSERT 语句中?

这是我所拥有的:

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 (mysql_error());
}

【问题讨论】:

    标签: php mysql foreign-keys primary-key


    【解决方案1】:

    假设您已经检索了 mem_id 和 post_id int php 变量,您需要执行以下操作:

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

    【讨论】:

      猜你喜欢
      • 2020-01-18
      • 2019-03-23
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 2016-09-11
      • 2012-07-12
      相关资源
      最近更新 更多