2013-03-05 46 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

相关问题