简体   繁体   中英

Inserting different variables into $_SESSION

I don't know if it's possible, but I'm trying to do the following:

in my page the user gets a list of groups from the 'groups' table in my Database. I want to make it so when the user clicks a group name he will get to a page called "group_page.php" but the page will have the relevant group info pulled from the table. I thought I could insert the chosen group name into $_SESSION and then call it in the "group_page.php" page, but it seem to set the first group name from the list into the $_SESSION regardless of which group I choose.
can I set the $_SESSION to different group names as I try bellow?

  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>";
}
?>

Inserting big data into $_SESSION is usually bad idea and is potential security point of break. You should try to pass it from model to view directly. But if you want to try this code:

$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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM