drtkyykai004574380 2015-07-25 09:18
浏览 29
已采纳

无法将用户详细信息存储到$ _SESSION中

I've been wracking my brain for days trying to store user details fetched from a database query in to a $_SESSION variable.

I put it on the backburner whilst I finished building another part of the site and used dummy data that I typed in to test it.

The code works fine with the dummy data, but doesn't run when I try to implement the results of the query in to it. I've tried following how other people set this up, but mine just doesn't work.

Here is the script I have with dummy data that works:

$query = "SELECT * FROM members WHERE Email='$Email' AND Activation IS NULL";
$result = $dbc->query($query);
$password_match = 0;

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $password_match = password_verify($Password, $row["Password"]);
        }
        if ($password_match) {
            $_SESSION["Username"] = "Members username"; 
            header("Location: page.php");
        }
        else {
            $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect';
        }
}

The dummy data here is "Members username", I would love for it to store the value retrieved as part of the query from the column "Username".

I've tried a number of ways, most recently was using this:

$_SESSION["Username"] = $row["Username"];

But had no luck with this.

Any pointers much appreciated!

  • 写回答

1条回答 默认 最新

  • douxiyi2418 2015-07-25 09:36
    关注

    Try this... You are not fetching row from inside loop,

    $query = "SELECT * FROM members WHERE Email='$Email' AND Activation IS NULL";
    $result = $dbc->query($query);
    $password_match = 0;
    
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $password_match = password_verify($Password, $row["Password"]);
    
            if ($password_match) {
                $_SESSION["Username"] = "Members username"; 
                header("Location: page.php");
            }
            else {
                $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect';
            }
    }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里