如何制作一个搜索函数,从我的sql数据库中搜索表


How to make a search function to search through a table from a my sql database

我现在有一个页面,显示mysql数据库中"学生"表中的所有记录,我需要一个功能,允许用户在表中选择任何变量,并显示一个搜索框,允许用户按相应的变量进行搜索。

例如,一个下拉框,其中包含学生表中的所有变量,在选择变量时,会出现一个文本框,允许用户输入一些文本,并在表中进行搜索。

目前,我的代码只是显示学生表中的所有记录。我不知道从哪里开始使用搜索功能。我尝试了很多例子,但每次我破坏代码时,我都会用到。

任何帮助都将不胜感激:)

<?php
$dbQuery = $db->prepare("select * from student order by Forename asc");
$dbQuery->execute();
$numStudent = $dbQuery->rowCount();
echo "<p>There are $numStudent students in the system</p>";
if (isset($lastInserted)) {
    echo "<p>The last student added has ID=$lastInserted</p>";
}
$oddRow=true;
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
    $ID       = $dbRow['ID'];
    $Forename = $dbRow['Forename'];
    $Surname  = $dbRow['Surname'];
    $Email  = $dbRow['Email'];
    $B_number = $dbRow['B_number'];
    $School = $dbRow['School'];
    $Campus = $dbRow['Campus'];
    $Research_Institute = $dbRow['Research_Institute'];
    $FTPT = $dbRow['FTPT'];
    if ($oddRow) $rowClass="odd"; else $rowClass="even";
    $oddRow=!$oddRow;
        echo "<tr class='$rowClass'><td>$Forename</td><td>$Surname</td><td>$Email</td><td>$B_number</td><td>$School</td><td>$Campus</td><td>$Research_Institute</td><td>$FTPT</td>
                  <td class='operation'>
              </tr>";
}

?>
</table>

</body>
</html>

您可能希望更新查询以显示类似内容。如果只想筛选表中的某些行,则需要where

select * 
  from student 
 where Forename = 'searchterm'
    or Surname = 'searchterm'
    or School = 'searchterm'
     order by Forename asc