0

如何在我的消息表中为 users.MM_Division = Division.divisionid 的每个 user.ID 插入表条目

我想在 messages.messagesuserid 中插入用户 ID 值

我想我需要一个while循环,但我不确定在处理插入查询时如何实现它。

目前,只有 1 条消息行被插入到消息表中的消息用户 ID 值帖子为 NULL

谢谢

这是我的代码:

session_start();

$colname_division = "-1";
if ((isset($_GET['divisionid'])) && ($_GET['divisionid'] != "")) {
      $colname_division = $_GET['divisionid'];

mysql_select_db($database_connect, $connect);
$query_users = sprintf("SELECT users.*, division.* FROM users INNER JOIN division ON division.divisionid = users.MM_Division WHERE divisionid = %s", GetSQLValueString($colname_users, "int"));
$users = mysql_query($query_users, $connect) or die(mysql_error());
$row_users = mysql_fetch_assoc($users);
$totalRows_users = mysql_num_rows($users);


// MESSAGE USERS DIVISION ARCHIVED
    $messageinsertSQL = sprintf("INSERT INTO messages (message, messageuserid) VALUES ('The division " . $row_division['division'] . " has been archived by " . $_SESSION['MM_Username'] . " please select a new division', '" . $row_users['ID'] . "')",GetSQLValueString($_GET['divisionid'], "int")); 
    mysql_select_db($database_connect, $connect);
    $Result4 = mysql_query($messageinsertSQL, $connect) or die(mysql_error());


// REDIRECT
    $updateGoTo = "../divisions.php";
        if (isset($_SERVER['QUERY_STRING'])) {
            $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
            $updateGoTo .= $_SERVER['QUERY_STRING'];
        }
    header(sprintf("Location: %s", $updateGoTo));

    mysql_free_result($division);
    exit;
}
4

1 回答 1

0

用以下内容包裹该message users division archived部分:

while (($row_users = mysql_fetch_assoc($users)) !== false) {
 $messageInsert = ...
 [...]
}

然后你需要删除$row_users = mysql_fetch_assoc($users)之前使用的。

顺便说一句,如果某些列具有相同的名称(来自用户和部门),我不确定它们是否不会被覆盖$query_users =$colname_users此外,该行在函数中有错误的参数GetSQLValueString()(不应该是$colname_division?或者你在其他地方声明它?)。

于 2012-05-21T13:24:07.200 回答