【问题标题】:How to insert lastInsertId using foreach loop in PHP and MySQL?如何在 PHP 和 MySQL 中使用 foreach 循环插入 lastInsertId?
【发布时间】:2020-03-09 08:00:08
【问题描述】:

早安,

我有这个 foreach 循环,一个变量值 (p_id) 从另一个表中分配给前一个插入的 lastInsertId()。假设我有两个表,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

假设我在 table_a 中添加了一个新人,他的名字是 Jerry,他的 ID 或 p_id 是 333。所以由于 Jerry 是最后一个包含在列表中的人,他的 p_id 是 333 现在是 lastInsertId() .然后假设 Jerry 拥有两本书,我现在将它们记录到 table_b 这是一个人拥有的书籍列表。

这是我将如何使用动态添加/删除输入框插入图书名称和类型的代码。

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

问题是我只能将 lastInsertId() 插入一次,ID 333 到列 p_id。

为了帮助您在此处进行可视化,这里是 table_b 的示例。

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

现在的问题是当我插入第二个数据或 Jerry 拥有的第二本书时。

这就是发生的事情。

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

我怎样才能使上面的表格看起来像这样,具有相同的 p_id。

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

谢谢。

【问题讨论】:

    标签: php mysql arrays


    【解决方案1】:

    在再次调用最后一个 ID 之前,您需要先保存/存储 Jerry 的 ID。

    所以不要将 ->lastInsertId() 包含在 foreach 块中。保存 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();
        }
    }
    

    【讨论】:

    • 旁注:您也不需要循环->prepare(),只需准备一次并循环绑定和执行。
    • @aaa28 很高兴这有帮助
    • 嘿,也许你也可以在这个问题上出点主意,我还不能在这里发布,因为显然我需要等待 90 分钟。这是链接:pastebin.com/fcEx9y2d
    • @aaa28 可能会更好,每本书保存一本书的一个实例,然后每个帖子输入循环它以保存每次迭代
    猜你喜欢
    • 1970-01-01
    • 2012-06-19
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2012-12-20
    相关资源
    最近更新 更多