将不同的变量插入$ _SESSION

时间:2014-08-13 09:48:53

标签: php mysql session

我不知道是否可能,但我正在努力做到以下几点:

在我的页面中,用户从我的数据库中的“groups”表中获取一个组列表。 我想这样做,当用户点击一个组名时,他将进入一个名为“group_page.php”的页面,但该页面将从表中提取相关的组信息。 我以为我可以将选择的组名插入$ _SESSION,然后在“group_page.php”页面中调用它,但它似乎将列表中的第一个组名设置为$ _SESSION,无论我选择哪个组。
我可以尝试将$ _SESSION设置为不同的组名吗?

  session_start();
    include_once("php/connect.php");

$result = mysqli_query($conn, "
    SELECT *
    FROM groups
    INNER JOIN users_groups ON groups.id = users_groups.group_id
    INNER JOIN users ON users.id = users_groups.user_id
    WHERE users.username = '$username' AND users_groups.approved = 1;
");

while($row = mysqli_fetch_array($result)) {
    $_SESSION['name'] = $row['name'];
    echo "<a href='index.php?target=group_page'><div class='group'>";
    echo "<h3 style='padding:20% 0%;'>" . $row['name'] . "</h3>";
    echo "</div>";
}
?>

1 个答案:

答案 0 :(得分:0)

将大数据插入$ _SESSION通常是个坏主意,并且是潜在的安全点。您应该尝试将其从模型传递到直接查看。但是如果你想尝试这个代码:

$names = array();
while($row = mysqli_fetch_array($result)) {
    $names[] = $row['name'];
    echo "<a href='index.php?target=group_page'><div class='group'>";
    echo "<h3 style='padding:20% 0%;'>" . $row['name'] . "</h3>";
    echo "</div>";
}
$_SESSION['name'] = $names;