无法获取会话变量以保存到我的数据库中

时间:2016-07-08 07:01:34

标签: php session

任何人都可以帮助我从会话中获取group_id并保存到数据库中,似乎没有工作,现在已经工作了一段时间。我得到的错误是注意:未定义的索引:group_name  这是我的剧本     

include('db.php')  

//Get User Info

    if(isset($_SESSION['username'])){
    $LoggedUser = $_SESSION['username'];
    if($GetUser = $mysqli->query("SELECT * FROM users WHERE    username='$LoggedUser'")){
    $UserInfo = mysqli_fetch_array($GetUser);
    $LoggedUsername = strtolower($UserInfo['username']);
    $LoggedUserLink   = preg_replace("![^a-z0-9]+!i", "-", $LoggedUsername);
    $LoggedUserLink   = strtolower($LoggedUserLink);
    $UserId = $UserInfo['user_id'];
    $GetUser->close();

}else{

     printf("Error: %s\n", $mysqli->error);

}
}
//Get Group info
if(isset($_SESSION['group_name'])){
$LoggedGroup = $_SESSION['group_name'];
if($GetGroup = $mysqli->query("SELECT * FROM groups WHERE group_name='$LoggedGroup'")){
    $GroupInfo = mysqli_fetch_array($GetGroup);
    $LoggedGroupname = strtolower($GroupInfo['group_name']);
    $LoggedGroupLink   = preg_replace("![^a-z0-9]+!i", "-", $LoggedGroupname);
    $LoggedGroupLink      = strtolower($LoggedGroupLink);
    $GroupId = $GroupInfo['group_id'];
    $GetGroup->close();
}else{
     printf("Error: %s\n", $mysqli->error);
}
   }

//获取变量并插入数据库

if($_POST)
{   
    $User     = $UserId;
    $Group    = $GroupId;
    $mysqli->query("INSERT INTO tb_name(group_id_fk, user_id_fk) VALUES ('$Group', '$User')");

     die('<div class="alert alert-success" role="alert">You have been added successfully to the group.</div>');
}else{
    die (mysqli_error());
}
?>

谢谢

2 个答案:

答案 0 :(得分:0)

使用 session_start() 功能启动会话。

会话变量使用PHP全局变量设置:$_SESSION

<?php
// Start the session
session_start();

// Set session variables
 if(isset($_SESSION['username'])) {

    ---- Your Statements -----

 }
?>

答案 1 :(得分:0)

在使用$ _SESSION global

之前,您需要先@Override public void render () { super.render(); // This is very important!!!!!!!! Gdx.gl.glClearColor(0, 0, 0.1f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); } 开始会话