2015-12-15 57 views
1

我想顯示數據庫中包含用戶輸入的相同單詞的所有數據。代碼正在工作,但問題是它只顯示一個結果。例如,如果我輸入單詞'ACTIVE',它只返回一個結果。但我有兩個數據,其中有'ACTIVE'字樣。我不知道該怎麼做。請幫助我這是爲了我的項目。使用關鍵字從數據庫顯示數據

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
    <h3>Search Lecturer</h3> 
    <input class="text" type="text" name="search" placeholder="Search..."/> 
    &nbsp;&nbsp;<input class="submit" type="submit" name="submit"> 
    <hr/> 
    <h3>Details</h3> 
</div> 
    <table class="table-fill"> 
    <thead> 
     <tr> 
      <th class="text-left">Lecturer ID</th> 
      <th class="text-left">Name</th> 
      <th class="text-left">Email</th> 
      <th class="text-left">Phone</th> 
      <th class="text-left">Status</th> 
      <th class="text-left">Gender</th> 
      <th class="text-left">Address</th> 
      <th class="text-left"></th> 
     </tr> 
    </thead> 
    <tbody class="table-hover"> 
    <?php 
if($_SERVER["REQUEST_METHOD"] == "POST"){ 
if($_POST["submit"]){ 
    $search = mysqli_real_escape_string($connection, trim($_POST['search'])); 
    $query = "SELECT * FROM LECTURER WHERE LCT_ID LIKE '%$search%' OR LCT_NAME LIKE '%$search%' 
      OR LCT_PHONE LIKE '%$search%' OR LCT_STATUS LIKE '%$search%' OR LCT_EMAIL LIKE '%$search%' 
      OR LCT_GENDER LIKE '%$search%' OR LCT_ADD LIKE '%$search%'"; 
    $result = mysqli_query($connection , $query); 

    if(mysqli_num_rows($result) > 0){ 
     if($row = mysqli_fetch_assoc($result)){ 
      echo "<tr>"; 
      echo '<td class="text-left">'.$row['LCT_ID'].'</td>'; 
      echo '<td class="text-left">'. $row['LCT_NAME'].'</td>'; 
      echo '<td class="text-left">'. $row['LCT_EMAIL'].'</td>'; 
      echo '<td class="text-left">'. $row['LCT_PHONE'].'</td>'; 
      echo '<td class="text-left">'. $row['LCT_STATUS'].'</td>'; 
      echo '<td class="text-left">'. $row['LCT_GENDER'].'</td>'; 
      echo '<td class="text-left">'. $row['LCT_ADD'].'</td>'; 
      echo '<td class="text-left"><a href="lctdlt.php?id='.$row['LCT_ID'].'" onclick="ConfirmDelete()">Delete</a></td>'; 
      echo '</tr>'; 
     } 
    } 
    } 
} 
else{ 
    $result = mysqli_query($connection, "SELECT * FROM LECTURER"); 
    while($row = mysqli_fetch_array($result)) { 
     echo "<tr>"; 
     echo '<td class="text-left">'.$row['LCT_ID'].'</td>'; 
     echo '<td class="text-left">'. $row['LCT_NAME'].'</td>'; 
     echo '<td class="text-left">'. $row['LCT_EMAIL'].'</td>'; 
     echo '<td class="text-left">'. $row['LCT_PHONE'].'</td>'; 
     echo '<td class="text-left">'. $row['LCT_STATUS'].'</td>'; 
     echo '<td class="text-left">'. $row['LCT_GENDER'].'</td>'; 
     echo '<td class="text-left">'. $row['LCT_ADD'].'</td>'; 
     echo '<td class="text-left"><a href="lctdlt.php?id='.$row['LCT_ID'].'" onclick="ConfirmDelete()">Delete</a></td>'; 
     echo '</tr>'; 
    } 
} 
?> 

回答

0

替換:

if($row = mysqli_fetch_assoc($result)) 

有了:

while ($row = mysqli_fetch_assoc($result)) 
+0

感謝你洙多了,現在工作 –