php - 过滤 MySQL 表搜索

标签 php mysql

我正在尝试设置一个 PHP MySQL 搜索脚本,它可以让我打印我的成员(member)资格 MySQL 数据库表的内容,然后使用不同的条件过滤结果。

我的 MySQL 表中使用了以下字段:

委员会 ID 秩 姓 名 性别 地址 电子邮件 电话号码 事件状态

我想要两种方法来过滤数据: 1)使用包含所有可用排名的下拉菜单,我可以按成员排名过滤结果。 2) 使用包含所有可用 active_status 的下拉菜单,您可以仅按事件状态过滤结果。

我已经成功设置了搜索表单,并打印了所有MySQL表内容,只有按位置搜索部分过滤了我的结果,但没有active_status,过滤部分是一个挑战。这是我的 html 表和搜索脚本:

   <?php include("./includes/connnect.php");?>
   <!DOCTYPE HTML>
   <html>
   <body>
   <table>
        <thead>
          <tr>
            <td>ID</td>
            <td>position</td>
            <td>Last Name</td>
            <td>First Name</td>
            <td>Sex</td>
            <td>Address</td>
            <td><strong>Email</td>
            <td><strong>Phone Number</td>
            <td>Status</td>
          </tr>
        </thead>
        <tbody>
            <?php               
            if ($_REQUEST["position"]<>'') {
                $search_position = " AND position='".mysql_real_escape_string($_REQUEST["position"])."'";   
            }
            if ($_REQUEST["status"]<>'') {
                $search_status = " AND status='".mysql_real_escape_string($_REQUEST["status"])."'"; 
            }
             else {
                $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE committee_id>0".$search_position.$search_status;
            }

            $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
            if (mysql_num_rows($sql_result)>0) {
                while ($row = mysql_fetch_assoc($sql_result)) {
            ?>
              <tr>
                <td><?php echo $row["committee_id"]; ?></td>
                <td><?php echo $row["rank"]; ?></td>
                <td><?php echo $row["last_name"]; ?></td>
                <td><?php echo $row["first_name"]; ?></td>
                <td><?php echo $row["sex"]; ?></td>
                <td><?php echo $row["address"]; ?></td>
                <td><?php echo $row["email"]; ?></td>
                <td><?php echo $row["phone_number"]; ?></td>
                <td><?php echo $row["active_status"]; ?></td>
              </tr>
            <?php
                }
            } else {
            ?>
            <tr><td colspan="5">No results found.</td>
            <?php   
            }
            ?>
        </tbody>
    </table>
</body>
</html>

如果有任何建议可以帮助我实现这一目标,我将不胜感激。

谢谢。

最佳答案

这是因为当存在 $_REQUEST["status"] 时,变量 SQL 不会像您将其放在 else 语句中那样设置

if ($_REQUEST["status"]<>'') {
            $search_status = " AND status='" . 
                           mysql_real_escape_string($_REQUEST["status"])."'"; 
        }
         else { /// this is your problem

            $sql = "SELECT * FROM ".$SETTINGS["data_table"]." 
                     WHERE committee_id>0".$search_position . $search_status;
        }

将$sql从else中取出,然后将else取出。

关于php - 过滤 MySQL 表搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450599/

相关文章:

php - Sql 连接查询错误。有人请帮助我

PHP PDO 类同时执行

sql - 将字段存储为单一文本类型 JSON 字符串与拆分到单独表的性能影响

php - 使用 PHP 将 DESCRIBE TABLE Postgres 导出到 .txt 文件

单击 Javascript - 函数的两个部分,但一次只能工作一个

php - 计算所有单词,包括 php 字符串中的数字

php - 在 mysqlquery 和 php 方面需要帮助

javascript - Knex MySQL迁移 "Unhandled rejection Error: Transaction query already complete"

mysql - 分组依据——不对 NULL 进行分组并控制显示的记录

MYSQL 一个字段的总和取决于其他字段的值