Insert repeatedly from foreach loop

Tags: , ,

I have a problem I’m trying to insert data into a table except that I need to retrieve the id before

  $get_ = $db->prepare("SELECT * FROM  table
                        WHERE id = :id");
  $get_->execute(['id' =>$id]);

  $get_ = $get_->fetchAll();

  foreach ($get_member as $k) {

    $not = "INSERT INTO notifications(..., ..., ...,id)
    $q = $bdd->prepare($not);

        '..' => ,
        'id' => $k['id'] // from foreach loop


I want an insertion as many times as the loop returns the result.

thank you in advance


Why are you using a loop for something that SQL does natively? You should be using insert . . . select:

INSERT INTO notifications (..., ..., ...,id)
    SELECT :.., :..., :...,
    FROM table t;

Source: stackoverflow