使用关键字从db显示数据

时间:2015-12-15 16:27:03

标签: php mysqli

我想显示数据库中包含用户输入的同一个单词的所有数据。代码正在运行,但问题是它只显示一个结果。例如,如果我输入单词' 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>'; 
    }
}
?>

1 个答案:

答案 0 :(得分:0)

替换:

if($row = mysqli_fetch_assoc($result))

使用:

   while ($row = mysqli_fetch_assoc($result))