-2

我想向我网站上的多个用户发送通知。他们的 ID 在一个数组中 ($userinput)。$userinput 保存成员 ID,在每个值的开头用逗号分隔(如“7,1,2,3”)。

我想运行这样的查询:

$sqlnot = "INSERT INTO lb_notif (to_id, time_sent, from_id, subject, message) VALUES ('$user_id_here', now(), '$logOptions_id', '$title', 'You have been invited to this group chat: <a href=profilechat.php?id=$logOptions_id&chat=$act_item_id>Click here to enter</a>.') ";

我怎么能把这个发送给所有这些成员?

4

3 回答 3

1
    $id_string = "7,1,2,3";     
    $user_ids = explode(",", $id_string);

    foreach($user_ids as $user_id_here){
         $sqlnot = "INSERT INTO lb_notif (to_id, time_sent, from_id, subject, message) VALUES ('$user_id_here', now(), '$logOptions_id', '$title', 'You have been invited to this group chat: <a href=profilechat.php?id=$logOptions_id&chat=$act_item_id>Click here to enter</a>.') ";
    // execute your query here
    }
于 2013-09-16T12:41:23.727 回答
1

您需要循环数组并动态构建查询:

$sqlnot = "INSERT INTO lb_notif (to_id, time_sent, from_id, subject, message) VALUES";
$parts = array();
foreach($userinput as $user) {
    $parts[] = "('$user', now(), '$logOptions_id', '$title', 'You have been invited to this group chat: <a href=profilechat.php?id=$logOptions_id&chat=$act_item_id>Click here to enter</a>.') ";
}
$sqlnot .= implode(',', $parts);
于 2013-09-16T12:44:20.207 回答
0

如果你想多次插入,那么 foreach 是一个魅力选项:

$ids = explode(",", $userinput);

foreach($ids as $user_id){
     $sqlnot = "INSERT INTO lb_notif (to_id, time_sent, from_id, subject, message) VALUES ('$user_id_here', now(), '$logOptions_id', '$title', 'You have been invited to this group chat: <a href=profilechat.php?id=$logOptions_id&chat=$act_item_id>Click here to enter</a>.') ";

}
于 2013-09-16T12:56:14.047 回答