2017-09-26 184 views
-1

我想閱讀我的個人資料頁面上的$ Session []變量,但我嘗試時遇到問題。 這是我的PHP腳本在我的login文件獲取存儲在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!'); 
     } 
     } 
    } 
    ?> 

在數據庫上的帳戶我有像用戶名,密碼,名字,姓氏,ADRESS,我想爲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> 

我怎麼能解決這個問題? 網頁包含PHP文件開頭

<?php session_start(); ?> 

會話和數據庫連接

<?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; 
    } 
    } 
}` 

我的目標是,以顯示已登錄的用戶的個人資料頁上的所有數據庫變量(全名,ADRESS,電話號碼),如果有人可以幫助我解決我的代碼,將是非常讚賞:)

+0

''我怎麼能解決這個問題'' - 什麼問題?什麼不工作?您的會話值是否不打印?你在哪裏設置這些會話值? – David

+0

例如,當我使用這個字符串

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

我收到錯誤 – Vezuure

+1

讀取錯誤消息將是一個不錯的第一步。 – David

回答

0

此頁面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> 
} 
+0

沒有爲我工作 – Vezuure

0

我收到這個解決方案的錯誤,我通過更改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> 

但它似乎似乎沒有出現,如果我重新加載屏幕。

看起來真的很奇怪我