dongzhi5587 2015-08-03 12:50
浏览 52
已采纳

如何在SQL数据库中基于php的两个参数进行过滤

I have a MySQL database, and the table I need to work with has 9 columns of information. My goal is to be able to filter, based on two arguments. For instance, the table is about students so it has data for first name, last name, id, course they are signed up for, status, occupation age and another 2 fields that are not that important. I need to be able to filter, based on the student's status and/or the course.

So far, I managed to get the php work done, with a form and a select tag, to filter based on status, but I have no idea how to add the second part. The done thing should be able to filter, based on status only, based on course only, or based on the selected status and course. The code looks like this:

if (isset($_POST['filter'])) {
        $search_term = mysqli_real_escape_string($conn, $_POST['filter_status']);
        $q .= " WHERE status = '$search_term'";
    }
    echo $q;
<form method="POST" action="index.php">
    <select name="filter_status" > 
        <option value="confirmed">confirmed</option>
        <option value="declined">declined</option>
        <option value="rejected">rejected</option>
        <option value="pending">pending</option>
        <option value="unconfirmed">unconfirmed</option>
    </select>
    <input type="submit" name="filter">
</form>

This works correctly, I have it a second time for the second criteria, but they don't work together.

  • 写回答

4条回答 默认 最新

  • duanmei4362 2015-08-03 14:35
    关注

    Thank you all for your input. It helped nudge me in the right direction. The code that ended up doing what I needed is as follows. It's not very elegant, but it does the job well:

    $q = "SELECT *
    FROM students";
    
    if (isset($_POST['filter'])) {
        if ($_POST['filter_status'] == null) {
        $search_term2 = mysqli_real_escape_string($conn, $_POST['filter_course']);
        $q .= " WHERE course = '$search_term2'";
    } elseif ($_POST['filter_course'] == null) {
        $search_term = mysqli_real_escape_string($conn, $_POST['filter_status']);
        $q .= " WHERE status = '$search_term'";
    } else {
        $search_term = mysqli_real_escape_string($conn, $_POST['filter_status']);
        $search_term2 = mysqli_real_escape_string($conn, $_POST['filter_course']);
        $q .= " WHERE status = '$search_term' AND course = '$search_term2'";
    }
    
    }
    

    And the form:

    <form method="POST" action="index.php">
        <select name="filter_status" > 
            <option value= ""></option>
            <option value="confirmed">confirmed</option>
            <option value="declined">declined</option>
            <option value="rejected">rejected</option>
            <option value="pending">pending</option>
            <option value="unconfirmed">unconfirmed</option>
        </select>
    
        <select name="filter_course">
        <option value= ""></option> 
            <option value="php">php</option>
            <option value="java">java</option>
        </select>
        <input type="submit" name="filter">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件