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 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败