Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 协助从数据库中提取当前记录的用户ID和名字_Php_Mysql_Session_Login - Fatal编程技术网

Php 协助从数据库中提取当前记录的用户ID和名字

Php 协助从数据库中提取当前记录的用户ID和名字,php,mysql,session,login,Php,Mysql,Session,Login,登录后,我正在构建一个学习计划器,其中只包含一个欢迎区。我当前正在尝试获取当前登录用户的用户ID,以用于此欢迎区域中的SQL update查询,以及用于欢迎消息中的用户名 我试着把$\u会话['user\u id']=userid和$\u会话['user\u firstname']=firstname在$\u会话['login\u user']=$username之后($\u SESSION['login\u user']=$username;可以正常工作),但是登录到页面后,我得到了以下错误:

登录后,我正在构建一个学习计划器,其中只包含一个欢迎区。我当前正在尝试获取当前登录用户的用户ID,以用于此欢迎区域中的SQL update查询,以及用于欢迎消息中的用户名

我试着把
$\u会话['user\u id']=userid
$\u会话['user\u firstname']=firstname
$\u会话['login\u user']=$username之后
($\u SESSION['login\u user']=$username;
可以正常工作),但是登录到页面后,我得到了以下错误:
注意:在第11行的C:\wamp64\www\justread\session.php中使用未定义的常量userid-假定为“userid”
注意:在第12行的C:\wamp64\www\justread\session.php中使用未定义的常量firstname-假定为“firstname”

现在,我知道这些错误希望我在使用“userid”和“firstname”设置会话变量之前先对它们进行某种初始化,但我不知道如何进行,所以我想知道是否有人可以帮助我

先谢谢你

如果需要,我可以发布更多代码,但我认为相关的代码有:

login.php:

<?php

// Start session
session_start();
// Variable to store error message
$error ="";
// If the login form (Note that the 'submit' refers to the 'name' attribute of the login form) has been submitted...
if (isset($_POST['submit'])) {
    // If username or password is not provided...
    if (empty($_POST['username']) || empty($_POST['password'])) {
        // ...tell user that login details are invalid.
        $error = "Please fill in both your username and your password";
        // Else...
    } else {
        // ...put the provided username and password in variables $username and $password, respectively
        $username = $_POST['username'];
        $password = $_POST['password'];
        // Establish connection to the server
        $mysqli = mysqli_connect("localhost", "root", "");
        // set up measures to counter potential MySQL injections
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysqli_real_escape_string($mysqli, $username);
        $password = mysqli_real_escape_string($mysqli, $password);
        // Select Database
        $db = mysqli_select_db($mysqli, "p00702");
        // SQL query to fetch information of registerd users and find user match.
        $query = mysqli_query($mysqli, "SELECT * from logins WHERE password='$password' AND username='$username'");
        // Return the number of rows of the query result and put it in $rows variable
        $rows = mysqli_num_rows($query);
        // If rows are equal to one...
        if ($rows == 1) {
            unset($_SESSION['error']);
            // Initialize session with the username of the user...
            $_SESSION['login_user'] = $username;
            // Set the user ID of the user
            $_SESSION['user_id'] = userid;
            // Set the user first name of the user
            $_SESSION['user_firstname'] = firstname;
            // ...and redirect to the homepage.
            header("Location: welcome.php");
            // Make sure that codes below do not execut upon redirection.
            exit;
        // Else, 
        } else {
            // and tell user that the login credentials are invalid.
            $error = "Your username or password is invalid";
            $_SESSION['error'] = $error;
            // redirect user to the home page (index.php)
            header("Location: index.php");
        }
        // ...and close connection
        mysqli_close($mysqli);
    }
}

在PHP中,变量名以“$”符号开头。此外,在login.php中,必须使用mysqli_fetch_row或任何类似函数获取数据。我假设您在登录后重定向到session.php。在这种情况下,您不必为会话变量分配任何内容。它已经在那里了。你所要做的就是访问它

login.php

<?php

// Start session
session_start();
// Variable to store error message
$error ="";
// If the login form (Note that the 'submit' refers to the 'name' attribute of the login form) has been submitted...
if (isset($_POST['submit'])) {
    // If username or password is not provided...
    if (empty($_POST['username']) || empty($_POST['password'])) {
        // ...tell user that login details are invalid.
        $error = "Please fill in both your username and your password";
        // Else...
    } else {
        // ...put the provided username and password in variables $username and $password, respectively
        $username = $_POST['username'];
        $password = $_POST['password'];
        // Establish connection to the server
        $mysqli = mysqli_connect("localhost", "root", "");
        // set up measures to counter potential MySQL injections
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysqli_real_escape_string($mysqli, $username);
        $password = mysqli_real_escape_string($mysqli, $password);
        // Select Database
        $db = mysqli_select_db($mysqli, "p00702");
        // SQL query to fetch information of registerd users and find user match.
        $query = mysqli_query($mysqli, "SELECT * from logins WHERE password='$password' AND username='$username'");
        // Return the number of rows of the query result and put it in $rows variable
        $rows = mysqli_num_rows($query);
        // If rows are equal to one...
        if ($rows == 1) {
            $row = mysql_fetch_object($query);
            unset($_SESSION['error']);
            // Initialize session with the username of the user...
            $_SESSION['login_user'] = $username;
            // Set the user ID of the user
            $_SESSION['user_id'] = $row->userid;
            // Set the user first name of the user
            $_SESSION['user_firstname'] = $row->firstname;
            // ...and redirect to the homepage.
            header("Location: welcome.php");
            // Make sure that codes below do not execut upon redirection.
            exit;
        // Else, 
        } else {
            // and tell user that the login credentials are invalid.
            $error = "Your username or password is invalid";
            $_SESSION['error'] = $error;
            // redirect user to the home page (index.php)
            header("Location: index.php");
        }
        // ...and close connection
        mysqli_close($mysqli);
    }
}

非常感谢您回复我。我已经输入了以下几行:$row=mysql\u fetch\u object($query)$_会话['user_id']=$row->userid$_会话['user_firstname']=$row->firstname;但它仍然会出现同样的错误。请检查session.php文件。一开始就不应该有那种任务操作。嘿,我有进展了。现在显示的是用户ID,我只需要对名字方面进行排序。我会让你知道的。再次感谢你。我现在已经整理好了,非常感谢你。愿你在需要时也能得到帮助。
<?php

// Start session
session_start();
// Variable to store error message
$error ="";
// If the login form (Note that the 'submit' refers to the 'name' attribute of the login form) has been submitted...
if (isset($_POST['submit'])) {
    // If username or password is not provided...
    if (empty($_POST['username']) || empty($_POST['password'])) {
        // ...tell user that login details are invalid.
        $error = "Please fill in both your username and your password";
        // Else...
    } else {
        // ...put the provided username and password in variables $username and $password, respectively
        $username = $_POST['username'];
        $password = $_POST['password'];
        // Establish connection to the server
        $mysqli = mysqli_connect("localhost", "root", "");
        // set up measures to counter potential MySQL injections
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysqli_real_escape_string($mysqli, $username);
        $password = mysqli_real_escape_string($mysqli, $password);
        // Select Database
        $db = mysqli_select_db($mysqli, "p00702");
        // SQL query to fetch information of registerd users and find user match.
        $query = mysqli_query($mysqli, "SELECT * from logins WHERE password='$password' AND username='$username'");
        // Return the number of rows of the query result and put it in $rows variable
        $rows = mysqli_num_rows($query);
        // If rows are equal to one...
        if ($rows == 1) {
            $row = mysql_fetch_object($query);
            unset($_SESSION['error']);
            // Initialize session with the username of the user...
            $_SESSION['login_user'] = $username;
            // Set the user ID of the user
            $_SESSION['user_id'] = $row->userid;
            // Set the user first name of the user
            $_SESSION['user_firstname'] = $row->firstname;
            // ...and redirect to the homepage.
            header("Location: welcome.php");
            // Make sure that codes below do not execut upon redirection.
            exit;
        // Else, 
        } else {
            // and tell user that the login credentials are invalid.
            $error = "Your username or password is invalid";
            $_SESSION['error'] = $error;
            // redirect user to the home page (index.php)
            header("Location: index.php");
        }
        // ...and close connection
        mysqli_close($mysqli);
    }
}
<?php

// Establish connection to the server
$mysqli = mysqli_connect("localhost", "root", "");
// Selecting Database
$db = mysqli_select_db($mysqli, "p00702");
// Starting session
session_start();

if (!isset($_SESSION['user_id'])) {
    // Closing Connection
    // Redirecting To Home Page
    header('Location: index.php');
    // Make sure that codes below do not execut upon redirection.
    exit;
}
print_r($_SESSION);