2

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.

0

1 Answer 1

2

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();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

sidenote: you don't need to loop the ->prepare() too, just prepare it once and loop the bind and execution.
@aaa28 sure glad this helped
hey maybe you can lend some of your brain on this one too, I cant post it yet here cause apparently I need to wait 90 minutes. Here is the link : pastebin.com/fcEx9y2d
@aaa28 might be better that each book saves one instance of a book, then each post input you loop it to save each iteration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.