user3129227
user3129227

Reputation: 51

PHP / MySQL Insert Multiple Records Based On Previous Query Results

I'm trying to develop a PHP script that notifies premium members of changes made to certain features of my website but I'm not sure how to add the multiple records to the notifications table. What I have is:

    $get_pro_sql = "SELECT id FROM members WHERE pro = '1'";
    $get_pro_res = mysqli_query($con, $get_pro_sql);

while($row = mysqli_fetch_assoc($get_pro_res){
    $memberid = $row["id"];
}
$message = "My New Message";
foreach($row){



    $insert_note_sql = "INSERT INTO notes(id, recipient, message, ..., ...) VALUES('', 'Member_Id__Returned_From_Previous_Query', '$message')";
    $insert_note_res = mysqli_query($con, $insert_note_sql);
    // Inserts 5 Records

Any ideas on how this can be achieved?

Upvotes: 0

Views: 150

Answers (1)

Attila
Attila

Reputation: 3406

You could this by using a sub query to insert multiple rows.

Actually you can achieve it like this:

INSERT INTO Results (People, names )
   SELECT d.id, 'Henry'
   FROM Names f
   JOIN People d ON d.id  = f.id

In the subquery you can select the actual values to be inserted.

More on this topic here

Upvotes: 1

Related Questions