duanhuang3074 2015-05-02 02:25
浏览 196
已采纳

如何搜索HTML复选框中选择的多个输入(单独)?

I have a simple question regarding structuring MySQLi Querys with multiple inputs for the same Table Column. Basically, I want people to be able to show results for (1) and (2) vs. (1 and 2), but I keep running into issues.

HTML:

<form method="get" action="page.php">
  <ul>
    <li><input type="checkbox" name="PT1" value="Condo">Condo</li>
    <li><input type="checkbox" name="PT2" value="Single Family">Single Family</li>
  </ul>
</form>

PHP:

if($_GET['PT1'] == "Condo") {
    $PropType1 = "(property_type = 'Condo') AND ";
}

if($_GET['PT2'] == "Single Family") {
     $PropType2 = "(property_type = 'Single Family') AND ";
}

$setPropType = $PropType1 . $PropType2;

MySQLi Statement:

$customSelectSQL = "SELECT * FROM $listingsTable WHERE $setPropType (listing_active = 'yes')";

I'm having a problem where if they're BOTH checked, the query does nothing and I get zero results. But when they're individually selected it works great.

I think what's happening is the database is searching for Properties that equal both inputs, as opposed to equaling each input separately and returning each row for each separate input. Make sense?

I'm guessing it's a matter of encapsulation or something, but when I search for "MySQL SELECT Multiple" or "MySQLi encapsulation", I get a bunch of not-so-helpful examples.

Thanks in advance!

  • 写回答

4条回答 默认 最新

  • doutun9179 2015-05-02 02:41
    关注

    Good thing would be to put these in the array and just implode them.
    edit: Also yeah, you should use OR while searching for property_type since you won't find 1 row with 2 different property_types as AND (since it is one column).

    Something like:

    $where = array();
    if($_GET['PT1'] == "Condo") {
            $where[] = "(property_type = 'Condo')";
    }
    if($_GET['PT2'] == "Single Family") {
            $where[] = "(property_type = 'Single Family')";
    }   
    
    $where_string = implode(' OR ', $where);
    $customSelectSQL = "SELECT * FROM $listingsTable WHERE (listing_active = 'yes') AND ($where_string)";
    

    edit2: To protect against empty array just add if (if there is only one AND needed) or add additional array to concatenate AND statements, like:

    $where_or = array();
    $where_and = array();
    $where_and[] = "(listing_active = 'yes')";
    if($_GET['PT1'] == "Condo") {
            $where_or[] = "(property_type = 'Condo')";
    }
    if($_GET['PT2'] == "Single Family") {
            $where_or[] = "(property_type = 'Single Family')";
    }   
    
    if(count($where_or) > 0) {
        $where_and[] = '('.implode(' OR ', $where_or).')';
    }
    
    if(count($where_and) > 0) {
        $where_string = implode(' AND ', $where_and);
    } else {
        $where_string = '';
    }
    $customSelectSQL = "SELECT * FROM $listingsTable $where_string";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请分析一下这个电路设计的优点🙏
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取