2013-03-05 106 views
1

我只需要顯示來自mysql數據庫的數據。用戶提交表單時,所有數據都已存儲在數據庫中。因此,在用戶登錄後的登錄頁面中,我需要顯示用戶全名以及數據庫表中的其他一些列。實質上,我希望頁面說歡迎全名,然後在表中顯示數據庫中的一些其他列。我應該如何使用會話進行編碼? 注:我曾嘗試使用會話登錄後顯示用戶的全名和當前餘額 我的代碼如下:使用會話在網頁中顯示來自mysql數據庫的數據

<?php 
    // Connect to database display welcome Full name, then date, name, credit, debit, balance 
    session_start(); 
    $fullname=""; 

    $currentbalance=""; 
    if (!isset($_SESSION)){ 
    session_start(); 
    } 
    echo $_SESSION['fullname']; 


    ?> 
    Welcome <?php echo $_SESSION['fullname']; ?>. 
    <table border ="1"> 
    <th>DATE </th> 
    <th>'.$fullname.' </th> 
    <th>CREDIT </th> 
    <th>DEBIT</th> 
    <th><?php echo $_SESSION['currentbalance']; ?</th> 
    </table> 
+1

需要分配值變量'$ _SESSION [「全名」] = $ fullname',你需要啓動會話使用前 – 2013-03-05 08:20:11

+0

確定我會嘗試,現在。那是我所沒有做的嗎? – Kaos 2013-03-05 08:22:49

回答

1
<?php 
    // Connect to database display welcome Full name, then date, name, credit, debit, balance 
    if (!isset($_SESSION)){ 
    session_start(); 
    } 
    $fullname=""; 
    $currentbalance=""; 

    $_SESSION['fullname']=$fullname; 
$_SESSION['currentbalance ']=$currentbalance ; // Where $fullname and $currentbalance must be already defined by the query 


    ?> 
    Welcome <?php echo $_SESSION['fullname']; ?>. 
    <table border ="1"> 
    <th>DATE </th> 
    <th>'.$fullname.' </th> 
    <th>CREDIT </th> 
    <th>DEBIT</th> 
    <th><?php echo $_SESSION['currentbalance']; ?</th> 
    </table> 
0

在登錄過程中只是將用戶ID存儲到會話,所以你可以很容易地得到了所有通過此用戶標識從數據庫獲取信息。

"select * from `users` where id='".$_SESSION['userid']."'" 
+0

好的,但我如何將用戶標識存儲到會話中?因爲用戶的全名只應在登錄後才顯示 – Kaos 2013-03-05 08:26:38

+0

如果您只是顯示全名,則將全名存儲在會話中。 – 2013-03-05 09:02:51

2

當您在您登錄需要存儲fullnamesession

$_SESSION['fullname'] = $_REQUEST['fullname']; 

login後,您可以在主頁上得到這個fullname

$session_name = $_SESSION['fullname']; 
0

使用此代碼,你把會議大括號內。

"select * from `users` where id={$_SESSION['userid']}" 
+1

請使用預準備語句,而不是直接將值放入查詢中。 – 2014-07-12 15:13:43

相關問題