2

I have a site for events and I'm trying to create a script that sends an alert to all members who are attending. This is done by selecting all of the members from the attendance table and adding a message for each of the resulting records. The code that I have at the moment is not working. Here is the code:

$get_attendance_sql = "SELECT attendee FROM attendance WHERE event = '$event_id'";
$get_attendance_res = mysqli_query($con, $get_attendance_sql);
while($row = mysqli_fetch_assoc($get_attendance_res)){
    $event_memberid = $row["attendee"];
}

foreach($row)
{
    $insert_membernote_sql = "INSERT INTO notes (id, recipient, type, subtype, link, date) VALUES('$id', '$event_memberid', 'cancel', 'event', '$event_id', '$time')";
    $insert_membernote_res = mysqli_query($con, $insert_membernote_sql);
}

3 Answers 3

2

Try this code.

You need to remove foreach and move insert query inside the while loop.

$get_attendance_sql = "SELECT attendee FROM attendance WHERE event = '$event_id'";
$get_attendance_res = mysqli_query($con, $get_attendance_sql);
while($row = mysqli_fetch_assoc($get_attendance_res)){
    $event_memberid = $row["attendee"];
    $insert_membernote_sql = "INSERT INTO notes (id, recipient, type, subtype, link, date) VALUES('$id', '$event_memberid', 'cancel', 'event', '$event_id', '$time')";
    $insert_membernote_res = mysqli_query($con, $insert_membernote_sql);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try this... you dont need another loop for insert, while fetching you can insert

$get_attendance_sql = "SELECT attendee FROM attendance WHERE event = '$event_id'";
$get_attendance_res = mysqli_query($con, $get_attendance_sql);
while($row = mysqli_fetch_assoc($get_attendance_res)){

$event_memberid = $row["attendee"];    
$insert_membernote_sql = "INSERT INTO notes (id, recipient, type, subtype, link, date)    VALUES('$id', '$event_memberid', 'cancel', 'event', '$event_id', '$time')";
    $insert_membernote_res = mysqli_query($con, $insert_membernote_sql);
}

Comments

1
<?php
$get_attendance_sql = "SELECT attendee FROM attendance WHERE event = '$event_id'";
$get_attendance_res = mysqli_query($con, $get_attendance_sql);
while($row = mysqli_fetch_assoc($get_attendance_res)){
    $event_memberid[] = $row["attendee"];
}

foreach($event_memberid as $memberid )
{
    $insert_membernote_sql = "INSERT INTO notes (id, recipient, type, subtype, link, date) VALUES('$id', '$memberid', 'cancel', 'event', '$event_id', '$time')";
    $insert_membernote_res = mysqli_query($con, $insert_membernote_sql);
}
?>

Comments

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.