2017-08-06 41 views
0

我經歷了多次試圖完成這個過程,但不能完全弄清楚。我繼續堆棧溢出找到重複的答案。我找到了一些並嘗試將它們調整爲我的代碼。仍然沒有用。我試圖說如果我已經登錄,請轉到個人資料頁面,如果用戶名在數據庫中,我希望你顯示他們的名字。如果不在數據庫中,則不顯示任何內容。以下是我想出了:一旦我登錄,試圖查看用戶名是否在數據庫中?

if (isset($_SESSION['user_id'])){ 
    $username = mysqli_real_escape_string($con, $_POST['username']); 
    $sql = "SELECT * FROM users WHERE username = '".$username."'"; 
    $result = mysqli_query($con,$sql); 
    if(mysqli_num_rows($result)>=1){ 
     echo "User was found in the database"; 
    } 
    else{ 
     echo "User was not found in the database."; 
    } 
} 

所以如果isset USER_ID基本上是說,如果我在我登錄,然後做到這一點。之後的代碼試圖查找是否在該數據庫中找到用戶名。如果是這樣,那麼說它被發現。如果不是,那麼不要。我希望這很清楚!謝謝!

編輯:下面是HTML代碼:

<?php 
    session_start(); 
    ob_start(); 
    include_once('dbconnect.php'); 
    ?> 

     <div class="banner_container"> 
      <div class="jumbotron text-center"> 
     <?php 


     if (isset($_SESSION['user_id']) && isset($_POST['username'])){ 

    if(($_SESSION['user_id'] != "") && ($_POST['username'] != "")){ 
     $user_id = $_SESSION['user_id']; 
     $username = mysqli_real_escape_string($con, $_POST['username']); 
     $sql = "SELECT * FROM users WHERE username = '".$username."' AND user_id !=".$user_id; 
     $result = mysqli_query($con, $sql); 
     if(mysqli_num_rows($result) > 0){ 
      echo "User was found in the database"; 
     } 
     else{ 
      echo "User was not found in the database."; 
     } 
    } 
    else{ 
     echo "Username or user Id is empty"; 
    } 

} 
     ?> 

     <?php 
      echo "<h1>"; 
      echo $_SESSION['first_name']; 
      echo " "; 
      echo $_SESSION['last_name']; 
      echo "</h1>"; 
      echo "<p>"; 
      echo '"'; 
      echo $_SESSION['quote']; 
      echo '"'; 
      echo "<br>"; 
      echo $_SESSION['who']; 
      echo "</p>"; 

     ?> 
     </div> 
    </div> 


<li><a href="http://www.quotin.co">Home</a></li> 
        <li><a href="quotin_about"> About</a></li> 
        <li class="qotd"><a href="quotin_qotd"> Quote of the Day</a></li> 
        <li class="all_categories"><a href="quotin_categories">All Categories</a></li> 
        <li><a href="http://www.quotin.co/quotin_authors"> Authors</a></li> 
        <?php 
         if(isset($_SESSION['user_id'])){ 
         echo $_SESSION['user_id']; 

         echo '<li id="active" class="dropdown">'; 
         echo '<a id="act_color" href="#" class="dropdown-toggle" data-toggle="dropdown">'; 
         echo $_SESSION['first_name']; 
         echo "'s"; 
         echo ' '; 
         echo "Profile"; 
         echo '<b class ="caret"></b></a>'; 
         echo '<ul class="dropdown-menu">'; 
         echo '<li><a href="profile.php"> Profile</a></li>'; 
         echo '<li><a href="logout.php">Log out</a></li>'; 
         echo '</ul>'; 
         echo '</li>'; 
         echo '</a>'; 
         echo '</li>'; 
         } else { 
         } 
        ?> 

這是我的個人資料頁。

回答

0

使用此代碼:

if (isset($_SESSION['user_id']) && isset($_POST['username'])){ 

    if(($_SESSION['user_id'] != "") && ($_POST['username'] != "")){ 
     $user_id = $_SESSION['user_id']; 
     $username = mysqli_real_escape_string($con, $_POST['username']); 
     $sql = "SELECT * FROM users WHERE username = '".$username."' AND user_id !=".$user_id; 
     $result = mysqli_query($con, $sql); 
     if(mysqli_num_rows($result) > 0){ 
      echo "User was found in the database"; 
     } 
     else{ 
      echo "User was not found in the database."; 
     } 
    } 
    else{ 
     echo "Username or user Id is empty"; 
    } 

} 
+0

我得到一個'未定義指數:username'錯誤和'mysqli_num_rows()預計參數1被mysqli_result' @ChandraKumar所有你需要檢查$ _ POST的 –

+0

第一['用戶名']有空或沒有.. –

+0

好吧,只要你說我很快研究了例子。老實說,我不明白。你能幫我檢查$ _POST ['username']是否爲空嗎? –

相關問題