0

我基本上希望它只选择登录的当前用户,然后在那里显示用户名和点,而不是显示数据库中的所有用户

这是显示它的代码

    <?php
include("connect.php"); // Includes the file connect.php to connect to database
session_start(); // Starting session cookies
if($_SESSION['LOGGEDIN'] == 1)  //Checking if they have the session cookie
{


$result = mysql_query("SELECT * FROM userdata");

echo "<table border='1'>
<tr>
<th>Username</th>
<th>Points</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['points'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
}

else
{
    echo "<title>Error!</title>";
    //Doesn't have session cookie
    echo "YOU ARE NOT LOGGED IN!";
}
?>
4

2 回答 2

4

好吧,您正在选择整个表格。

只需将其更改为

$result = mysql_query("SELECT * FROM `userdata` WHERE `username`='".$_SESSION["LOGGEDIN"]."' LIMIT 1");

请注意,您应该更改会话变量LOGGEDIN以包含登录用户的用户名,或者使用另一个会话变量并替换LOGGEDIN我上面代码行中的引用。

例如,在您的登录脚本中,而不是执行以下操作:

if($_POST["user"] == $user and $_POST["password"] == $pass)
$_SESSION["LOGGEDIN"] = 1;

做这个:

if($_POST["user"] == $user and $_POST["password"] == $pass)
$_SESSION["LOGGEDIN"] = $user;

如果你使用LOGGEDIN,你需要更新你的初始 if 子句,这样它就不会检查它是否等于一,而是检查它是否被设置:

if(isset($_SESSION["LOGGEDIN"]))

所以你的文件应该是这样的:

<?php
include("connect.php"); // Includes the file connect.php to connect to database
session_start(); // Starting session cookies
if(isset($_SESSION['LOGGEDIN']))  //Checking if they have the session cookie
{


$result = mysql_query("SELECT * FROM `userdata` WHERE `username`='".$_SESSION["LOGGEDIN"]."' LIMIT 1");

echo "<table border='1'>
<tr>
<th>Username</th>
<th>Points</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
}

else
{
    echo "<title>Error!</title>";
    //Doesn't have session cookie
    echo "YOU ARE NOT LOGGED IN!";
}
?>
于 2012-11-26T05:20:38.930 回答
0
<?php 

        include("connect.php");
        $email= $_POST['userid'];
        $password= $_POST['password1']; 
        $papas=base64_encode($password);
        $check = $_POST['rememberme'];      
            $tablename="userdata";
        $select_qry = $jeob->SqlQuery("SELECT * FROM ".$jeob->dbprefix.$tablename." WHERE email ='$email' AND password ='$papas' AND active_link='1' ");
            if($jeob->SqlRows($select_qry) == "0"){                 
             echo "Invalid Username and Password";
            } else {

            $getuser = $jeob->SqlFetch($select_qry);
            $_SESSION['userid'] = $getuser['user_id'];  
            $_SESSION['oauth_provider'] = "normal";
            $_SESSION['email'] = $getuser['email']; 
            }
                if($_SESSION['userid'] == ""
                {
                echo "You are not logged in";
                }
                else
                {
                Welcome "Fetch username using the session id or emial"
                }
    ?>

希望这对你有用

于 2012-11-26T05:45:57.517 回答