$ _SESSION [' id']为空

时间:2016-04-21 17:44:25

标签: php mysql session

我正在尝试在php脚本中更新密码,但是$ _SESSION [' id']为空。这通过var_dump函数显示。因此我无法更新

<?php
session_start();
require "../init.php";
ini_set('display_errors', 1);


if(isset($_POST['update'])){
    $password = $_POST['user_pass'];
    $passwordEncrypted = sha1($user_pass); 

    $confpassword = $_POST['confirm_pass'];
    $confPasswordEncrypted = sha1($confirmPass);  
    if($password !== $confpassword){
       echo "Passwords don't match!";
    }else{
        $select_query = "SELECT id FROM user_info";
        $run_select_query = mysqli_query($con,$select_query); 
        while ($row_post=mysqli_fetch_array($run_select_query)){

            $_SESSION['id'] = $row['id'];
            $user_id = $_SESSION['id'];
            var_dump($user_id);

        }

        $update_posts = "UPDATE user_info SET user_pass='$passwordEncrypted',confirm_pass ='$confPasswordEncrypted' WHERE id='$user_id'";  
        $run_update = mysqli_query($con,$update_posts); 
        echo "<script>alert('Post Has been Updated!')</script>";
    }

}
?>

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:2)

在为$row变量赋值时,您使用了错误的变量session

while ($row_post=mysqli_fetch_array($run_select_query)){
    $_SESSION['id'] = $row_post['id'];
    $user_id = $_SESSION['id'];
    var_dump($user_id);
}