用户登录并注册后如何显示“名称”和“电子邮件”之类的数据

我是编码的新手。我正在尝试在接下来的几页上显示用户已保存(注册时)的某些数据。我可以显示用户名,但也要显示名称和电子邮件。


这是用户登录时的时间。


    $query = "SELECT * FROM users 

                WHERE username='$username' 

                AND password='$password'";

    $results = mysqli_query($db, $query);

    if (mysqli_num_rows($results) == 1) {

        $_SESSION['username'] = $username;

        $_SESSION['success'] = "You are now logged in";

        header('location: indexclient.php');

    }else {

        array_push($errors, "Wrong username/password combination");

    }


    //new page

    //This is the top of the page where I want to display the name.


    session_start();


    if (!isset($_SESSION['username'])) {

        $_SESSION['msg'] = "You must log in first";

        header('location: loginclient.php');

    }


    if (isset($_GET['logout'])) {

        session_destroy();

        unset($_SESSION['username']);

        header("location: loginclient.php");

    }


    //This is where I want to display the name at the bottom of the page:


  <h2> <?php echo $_SESSION['username']; ?>'s Profile</h2>

  <?php echo $_SESSION['firstname']; ?>


  <?php  if (isset($_SESSION['username'])) {

   echo "Hello",  $_SESSION['firstname']  ;

  }


白猪掌柜的
浏览 268回答 2
2回答

九州编程

您需要从数据库获取数据并将其存储在变量中,以便可以在任何需要的地方使用它们。$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";$results = mysqli_query($db, $query);while($data = mysqli_fetch_array($results)) {&nbsp; $name = $data['name_from_db']; //Here you put a name of column where you store name in DB&nbsp; $email = $data['email_from_db']; //Here you put a name of column where you store email in DB&nbsp;// You can put in session if you want to have it globaly}if (mysqli_num_rows($results) == 1) {&nbsp; &nbsp; $_SESSION['username'] = $username;&nbsp; &nbsp; $_SESSION['success'] = "You are now logged in";&nbsp; &nbsp; $_SESSION['name'] = $name ; //This is example where I put in SESSION variable&nbsp; &nbsp; $_SESSION['email'] = $email; //This is example where I put in SESSION variable&nbsp; &nbsp; header('location: indexclient.php');} else {&nbsp; &nbsp; array_push($errors, "Wrong username/password combination");}请注意,在此代码中,您没有为安全做任何事情,它很容易受到攻击。请阅读有关参数化查询和防止SQL注入的保护,以及有关密码哈希的信息。用纯文本发送密码是不安全的!

隔江千里

在此部分:if (mysqli_num_rows($results) == 1) {&nbsp; &nbsp; $_SESSION['username'] = $username;&nbsp; &nbsp; //Add another entry in SESSION here&nbsp; &nbsp; $_SESSION['success'] = "You are now logged in";&nbsp; &nbsp; header('location: indexclient.php');}您可以添加一个新条目,例如 $_SESSION['email'] = $result[index_of_email_in_results]
打开App,查看更多内容
随时随地看视频慕课网APP