【问题标题】:How to make a search function to search through a table from a my sql database如何制作搜索功能以搜索mysql数据库中的表
【发布时间】:2015-03-19 20:34:25
【问题描述】:

我现在有一个页面显示我的 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>

【问题讨论】:

  • 非常笼统的问题,上面的窗口粘贴了太多代码...我确定您正在寻找一些 JavaScript / AJAX 解决方案,但您至少需要开始构建它。跨度>

标签: php mysql forms search


【解决方案1】:

您可能希望更新您的查询以说出类似的内容。如果您只想过滤表中的某些行,则需要 where

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 2019-04-15
    • 2017-01-05
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多