简体   繁体   English

如何在 PHP 和 MySQL 中使用 foreach 循环插入 lastInsertId?

[英]How to insert lastInsertId using foreach loop in PHP and MySQL?

Good day,再会,

I have this foreach loop, one variable value (p_id) was assigned to the lastInsertId() of the previous insert, from the other table.我有这个 foreach 循环,一个变量值 (p_id) 被分配给前一个插入的 lastInsertId(),来自另一个表。 Let say I have two table, table_a for list of person, and table_b for the list of books they own.假设我有两个表,table_a 用于人员列表,table_b 用于他们拥有的书籍列表。

p_id – is the id of the person from table_a
b_title – is the title of the book
b_genre is the genre of the book

Let say I added a new person in table_a, his name is Jerry and his ID or p_id is 333. So since Jerry is the last person to be included in the list, his p_id which is 333 is now the lastInsertId().假设我在 table_a 中添加了一个新人,他的名字是 Jerry,他的 ID 或 p_id 是 333。因此,由于 Jerry 是最后一个包含在列表中的人,他的 p_id 是 333 现在是 lastInsertId()。 Then let say Jerry owns two books that I will now record to table_b which is the list of books own by a person.然后假设 Jerry 拥有两本书,我现在将它们记录到 table_b,这是一个人拥有的书籍列表。

Here is the code on how I will Insert the name and the genre of the book using a dynamic add/remove input box.这是有关如何使用动态添加/删除输入框插入书籍名称和类型的代码。

$query_1 = “some_mysql_code_that_will_insert_data_to_table_a”;

       $stmt_1 = $this->conn->prepare($query_1);

if($stmt_1->execute()){
//run this code under
              foreach($p_name AS $key => $value) {

                  $query_2 = "INSERT INTO

                    table_b

                    SET
                    p_id=:p_id,
                    b_title = :b_title,
                    b_genre = :b_genre";


                    $stmt _2= $this->conn->prepare($query);


                    $p_id=$this->conn->lastInsertId();
                    $b_title=$value;
                    $b_genre = $b_genre[$key];


                    $stm_2->bindParam(':p_id', $p_id);
                    $stm_2t->bindParam(':b_title', $b_title);
                    $stmr_2->bindParam(':b_genre', $b_genre);

                   $stmt_2->execute();
                  }
}

the problem is I can only insert the lastInsertId() once, the ID 333 to column p_id.问题是我只能将 lastInsertId() 插入一次,即 ID 333 到 p_id 列。

To help you visualize here are the sample of table_b.为了帮助您可视化这里是 table_b 的示例。

+--------+---------+---------+---------+
| b_id   | p_id    | b_title | b_genre |
+--------+---------+---------+---------+
| 1      | 333     | Carrie  | Horror  |

Now the problem is when I insert the second data or the second book that Jerry owns.现在的问题是当我插入第二个数据或杰瑞拥有的第二本书时。

Here is what happening.这是发生的事情。

+--------+---------+---------+---------+
| b_id   | p_id    | b_title | b_genre |
+--------+---------+---------+---------+
| 1      | 333     | Carrie  | Horror  |
+--------+---------+---------+---------+
| 2      | 1       | Dune    | Scifi   |

How can I make the table above, look like this, with same p_id.我怎样才能使上面的表格看起来像这样,具有相同的 p_id。

+--------+---------+---------+---------+
| b_id   | p_id    | b_title | b_genre |
+--------+---------+---------+---------+
| 1      | 333     | Carrie  | Horror  |
+--------+---------+---------+---------+
| 2      | 333     | Dune    | Scifi   |

Thank You.谢谢你。

You need to save / store Jerry's ID first, before you invoke the last ID again.在再次调用最后一个 ID 之前,您需要先保存/存储 Jerry 的 ID。

So don't include that ->lastInsertId() inside the foreach block.所以不要在foreach块中包含->lastInsertId() Save the ID, then reuse inside the loop.保存 ID,然后在循环内重复使用。

$query_1 = "some_mysql_code_that_will_insert_data_to_table_a";

$stmt_1 = $this->conn->prepare($query_1);

if ($stmt_1->execute()) {
    // run this code under

    $p_id = $this->conn->lastInsertId(); // save Jerry's ID here first

    $query_2 = "
    INSERT INTO table_b
    SET
        p_id=:p_id,
        b_title = :b_title,
        b_genre = :b_genre
    ";


    $stmt_2 = $this->conn->prepare($query);

    foreach($p_name AS $key => $value) {

        $b_title = $value;
        $b_genre = $b_genre[$key];


        $stmt_2->bindParam(':p_id', $p_id);
        $stmt_2->bindParam(':b_title', $b_title);
        $stmr_2->bindParam(':b_genre', $b_genre);

        $stmt_2->execute();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM