如何使用PHP将搜索结果插入表中?

时间:2018-03-21 23:03:01

标签: php mysql

这是我必须搜索的代码,它只是彼此相邻。我可以在PHP中放入html,这将如何做?或者我应该在表格下面写一张表?

   <?php

  include ('database_conn.php');

    $output = '';

    if(isset($_POST['search'])) {
      $search = $_POST['search'];
      $search = preg_replace("#[^0-9a-z]i#","", $search);

      $query = mysqli_query($conn, "SELECT * FROM attendance WHERE stud_id LIKE '%$search%'") or die ("Could not search");
      $count = mysqli_num_rows($query);

      if($count == 0){
        $output = "There was no search results!";

      }else{

        while ($row = mysqli_fetch_array($query)) {


          $stud_id = $row ['stud_id'];
          $module = $row ['module'];
          $attendance_status = $row ['attendance_status'];

          $output .='<div> '.$stud_id.''.$module.''.$attendance_status.'</div>';




        }

      }
    }

    ?>

这是我在HTML中搜索的表单

<form action ="CM0671_attendance.php" method = "post">

      <input name="search" type="text" size="30" placeholder="Student ID"/>

      <input class="btn btn-primary" type="submit" value="Search"/>

      </form> 

1 个答案:

答案 0 :(得分:0)

Replace your while with this:


echo "<table>";
    echo "<tr>";
        echo "<th>ID</th>";
        echo "<th>Module</th>";
        echo "<th>Status</th>";
    echo "</tr>";
while ($row = mysqli_fetch_array($query)) {
          $stud_id = $row ['stud_id'];
          $module = $row ['module'];
          $attendance_status = $row ['attendance_status'];
              echo "<tr>";
                echo "<td>{$stud_id}</td>";
                echo "<td>{$module}</td>";
                echo "<td>{$attendance_status}</td>";
              echo "</tr>";
        }
echo "</table><br>";