Query building depending checkboxes selection

Posted by user3661845 on Stack Overflow See other posts from Stack Overflow or by user3661845
Published on 2014-05-26T21:12:39Z Indexed on 2014/05/26 21:25 UTC
Read the original article Hit count: 83

Filed under:
|

I want to build a query form my database depending my checkboxes list.

My checkboxes:

<input type="checkbox" id="searchName" checked> Name
<input type="checkbox" id="searchAddress"> Address
<input type="checkbox" id="searchCompany"> Company
<input type="checkbox" id="searchComments"> Comments

My PHP:

$subQuery='';
if($_POST['searchName']=='true')     { $subQuery .= " AND KDX_Name LIKE :KDX_SearchTerm"; }
if($_POST['searchAddress']=='true')  { $subQuery .= " OR KDX_PostalAddress LIKE :KDX_SearchTerm"; }
if($_POST['searchCompany']=='true')  { $subQuery .= " OR KDX_Company LIKE :KDX_SearchTerm"; }
if($_POST['searchComments']=='true') { $subQuery .= " OR KDX_Comments LIKE :KDX_SearchTerm"; }

My problem:

If the first checkbox is not checked, my query is not working cause it works with OR whereas it must start with AND.

Could you please help ?

Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about sql