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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题