请注意,
您的代码对SQL注入相关的攻击开放。请学习使用预备报表
现在,我们将需要
WHERe动态生成查询的一部分。我们可以使用
!empty()函数检查输入的过滤器值是否不为空,然后将其条件动态添加到查询中。
$functie = $_POST['functie'];$branche = $_POST['branche'];$regio = $_POST['regio'];$search = "SELECt cnt.title, cnt.alias, cnt.images, cnt.introtext, cnt.catid, cat.title, cat.aliasFROM snm_content cntLEFT JOIN snm_categories catON cat.id = cnt.catid ";// Collect all the where conditions in an array$whr = array();// check if $functie has some value in input filterif (!empty($functie)) { $whr[] = "cnt.title LIKE '%" . $functie . "%'";}// check if $branche has some value in input filterif (!empty($branche)) { $whr[] = "cat.title LIKE '%" . $branche . "%'";}$where_sql = '';// Prepare where part of the SQLif (!empty($whr)) { $where_sql = ' WHERe ' . implode(' OR ', $whr);}// Append to the original sql$search .= $where_sql;


