简体   繁体   English

在 php 登录会话中获取存储的数据库变量

[英]get stored Database variables on php login session

i would like to read $Session[] variables on my profile page but im having issues while trying it.我想在我的个人资料页面上读取 $Session[] 变量,但我在尝试时遇到了问题。 this is my php script on my login file这是我的登录文件中的 php 脚本

<?php
    if (isset($_POST['LoginBtn']))
    {
      $username = $_POST['username'];
      $password = $_POST['password'];
      if (empty($username) || empty($password))
      {
        $show -> showError("please fill out all the fields");
      }
      else
      {
        $SQLCheckUser = $odb -> prepare("SELECT COUNT(*) FROM `Account` WHERE `username` = :user AND `password` = :password LIMIT 1");
        $SQLCheckUser -> execute(array(':user' => $username, ':password' => hash('SHA512', $password)));
        $loginCheck = $SQLCheckUser -> fetchColumn(0);
        if ($loginCheck)
        {

          $SQLGetID = $odb -> prepare("SELECT `ID`, FROM `Account` WHERE `username` = :username LIMIT 1");
          $SQLGetID -> execute(array(':username' => $username));

          $_SESSION['username'] = $username;

          $_SESSION['ID'] = $SQLGetID -> fetchColumn(0);
          $show -> showSuccess('Willkommen... <meta http-equiv="refresh" content="2;url=index.php">');
        }
        else
        {
          $show -> showError('user was not found!');
        }
      }
    }
    ?>

in the Database on Account i have variables like, username, password, firstname, lastname, adress and i would like to display them on the profile.php page for example like this在帐户数据库中,我有变量,如用户名、密码、名字、姓氏、地址,我想在 profile.php 页面上显示它们,例如像这样

          <h4 class="profile-user"><?php echo $_SESSION['firstname']; ?></h4>
          <p class="profile-job"><?php echo $_SESSION['lastname']; ?></p>
          <p class="profile-job"><?php echo $_SESSION['adress'];?></p>

how could i solve this issue ?我怎么能解决这个问题? page contains a php file starting a session with页面包含一个开始会话的 php 文件

<?php session_start(); ?>

and a db connection和一个数据库连接

<?php
$show = new show();
$user = new user($odb);
$status = new status($odb);
class show
{
function showError($error)
{
    echo '<div class="alert alert-danger"><a class="close" data-                                dismiss="alert" href="#">&times;</a><h4 class="alert-heading">error!</h4>'.$error.'</div>';
}
function showSuccess($success)
{
    echo '<div class="alert alert-success"><a class="close" data-dismiss="alert" href="#">&times;</a><h4 class="alert-heading">Login success!</h4>'.$success.'</div>';
}
}

class user
{
var $odb;

function __CONSTRUCT($odb)
{
    $this -> odb = $odb;
}
function loggedIn()
{
    if (isset($_SESSION['username'], $_SESSION['ID']))
    {
        return true;
    }
    else
    {
        return false;
    }
}
function isAdmin()
{
    $SQL = $this -> odb -> prepare("SELECT `admin` FROM `Account` WHERE `ID` = :id");
    $SQL -> execute(array(':id' => $_SESSION['ID']));
    $rank = $SQL -> fetchColumn(0);
    if ($rank == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
    }
}`

my goal is it to display all the database variables of the logged in user on the profile page (Full name, Adress , Phone number) if someone could help me fix my code that would be really appreciated :)我的目标是在个人资料页面上显示登录用户的所有数据库变量(全名、地址、电话号码),如果有人可以帮助我修复我的代码,将不胜感激:)

this page profile.php这个页面 profile.php

session_start();
$dbh = new PDO(" --- connection string --- ");
$user = new user($dbh);
if ($user->loggedIn()) {
    $stmt = $odb->prepare("SELECT `firstname`, `lastname`,`adress`,FROM `Account` WHERE ID=$_SESSION['ID'] LIMIT 1");
    $stmt->execute();
    $row = $stmt->fetch();
}
foreach ($row as $item)
{
    <h4 class="profile-user"><?php echo $item['firstname']; ?></h4>
    <p class="profile-job"><?php echo $item['lastname']; ?></p>
    <p class="profile-job"><?php echo $item['adress']; ?></p>
} 

i received an error with this solution, i fixed it by changing the code in the profile.php to this.我收到此解决方案的错误,我通过将 profile.php 中的代码更改为此进行了修复。

<div class="col-lg-3">
      <!-- Page Widget -->

     <?php 
     $stmt = $odb->prepare("SELECT 'vorname', 'nachname','email',FROM 'Account' WHERE ID=$_SESSION[ID] LIMIT 1");
     $stmt->execute();
     $row = $stmt->fetch();

     foreach ((array) $row as $item){
       $vorname = $item['vorname']; // german word for firstname
       $nachname = $item['nachname']; // german word for lastname
       $email = $item['email']; // german word for email

     } ?>
      <h4 class="profile-user"><?php echo $vorname ?></h4>

but it doesnt seem to appear if i reload the screen.但如果我重新加载屏幕,它似乎不会出现。

looks really weird to me对我来说看起来很奇怪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM