进入查询的用户所填写的唯一字段


the only fields filled by the user going to the query

我想从用户输入的sql中筛选数据。问题是我有3个字段,例如(职位、状态、雇主)。但如果用户没有输入某个字段(例如字段雇主),则该字段将不会包含在查询中(where子句)。

这是我的问题

$result_set = mysqli_query($connection, "
SELECT fname, mname, lname, contactno ...
FROM tblapplicant, tblreq
WHERE tblapplicant.appid = tblreq.appid AND stat= '$a' AND emp= '$b' AND hired_pos= '$c'
");

尝试使用OR而不是AND

SELECT fname,mname,lname,contactno ...
FROM tblapplicant 
inner join tblreq 
        on tblapplicant.appid=tblreq.appid 
where (stat= '$a' and  $a is not null)
   or (emp= '$b'  and $b is not null)
   or (hired_pos= '$c' and $c is not null)

您可以有条件地附加附加附加条件。

$sql = 'SELECT fname, mname, lname, contactno ...
FROM tblapplicant, tblreq
WHERE tblapplicant.appid = tblreq.appid ';
if ($a) { $sql .= "AND stat='$a' "; }
if ($b) { $sql .= "AND emp='$b' "; }
if ($c) { $sql .= "AND hired_pos='$c' "; }
$result_set = mysqli_query($connection, $sql);