2012-11-26 67 views
0

我基本上希望它選擇只記錄在當前用戶多數民衆贊成,然後顯示有用戶名和點,而不是它顯示數據庫中的所有用戶PHP,只能選擇在用戶名登錄,從數據庫

這是代碼,顯示它

<?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!"; 
} 
?> 
+1

哪裏是'where'在查詢子句.. –

+0

我想這就是問題所在。什麼我會選擇當前登錄的用戶嗎? –

+0

'當用戶名='$ _SESSION ['LOGGEDIN']'' –

回答

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,你需要的,如果更新您的初始以便它不檢查它是否等於1,而是檢查它是否設置:

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!"; 
} 
?> 
+0

嗨,當我把這個,它沒有顯示這一次。 –

+0

你有沒有做我在第一行代碼下面註明的內容?你需要讓'$ _SESSION [「LOGGEDIN」]存儲用戶的用戶名或者它不起作用,並且你還需要將你的if語句改爲我提供的語句。 – David

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" 
       } 
    ?> 

希望這是對你有用