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

Viewed 81

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. Let say I have two table, table_a for list of person, and table_b for the list of books they own.

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(). 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.

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.

To help you visualize here are the sample of 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.

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

Thank You.

1 Answers

You need to save / store Jerry's ID first, before you invoke the last ID again.

So don't include that ->lastInsertId() inside the foreach block. Save the ID, then reuse inside the loop.

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