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条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制