So I'm trying to send a message to all the users in the database. I saw an other article but there it was used for emails and has commas between it. I don't know but maybe there is some solution for this to properly insert data with all the users ID.
while($row = mysqli_fetch_array($result))
{
$users[] = $row['ID'];
}
$sendmes = "INSERT INTO notify (notfi, thumb_src, title, url, user_ID) VALUES ('$msg', '$src', '$title', '$url', '$users[]')";
This gives the Array(), but it seems not to work like this.
So I used
$user = implode(", ", $users);
$sendmes = "INSERT INTO notify (notfi, thumb_src, title, url, user_ID) VALUES ('$msg', '$src', '$title', '$url', '$user')";
After that it is still not working, this is because it uses commas between the IDs and only work for emails. After that I was searching google but couldn't find anything about it.
So my last hope is here and to help others with the same question.
"INSERT INTO notify (notfi, thumb_src, title, url, user_ID) VALUES ('$msg', '$src', '$title', '$url', '$users[]')"
- Here the value of the 'user_ID' should be a single user_id value, not an array.
You can check the following query:
while($row = mysqli_fetch_array($result))
{
$users[] = $row['ID'];
}
foreach ($users as $user_id)
{
$sendmes = "INSERT INTO notify (notfi, thumb_src, title, url, user_ID) VALUES ('$msg', '$src', '$title', '$url', '$user_id')";
// Execute the Query;
}
Another approach can be: Instead of single query for each user, if you want to insert them altogether, then you can change your query in the following way;
"INSERT INTO tbl_name (notfi, thumb_src, title, url, user_ID)
VALUES
('$msg', '$src', '$title', '$url', '$users[0]'),
('$msg', '$src', '$title', '$url', '$users[1]'),
('$msg', '$src', '$title', '$url', '$users[2]')
....
('$msg', '$src', '$title', '$url', '$users[(count($users) - 1)]')"
But in this approach if the query becomes too large, or if there's any error in any of the insertion the remaining insertion will fail.