我正在开发一种先进的搜索系统,可根据用户的技能搜索用户。这些是各种技能。
每项技能都存储在数据库中的单独列中,值为0& 1。
现在,当我搜索用户时,我使用OR
条件,这样当我检查Access时,将找到具有Access技能的用户。以下是搜索条件的代码:
$query .= "SELECT * FROM wordpress_candidate
WHERE (city = '$cty')
OR (state = '$stt')";
if(isset($s_g_1)){ $query .= "OR (generala = '$s_g_1')"; }
if(isset($s_g_2)){ $query .= "OR (generalb = '$s_g_2')"; }
if(isset($s_g_3)){ $query .= "OR (generalc = '$s_g_3')"; }
if(isset($s_g_4)){ $query .= "OR (generald = '$s_g_4')"; }
//if(isset($s_g_5)){ $query .= "OR (generale = '$s_g_5')"; }
if(isset($s_g_6)){ $query .= "OR (generale = '$s_g_6')"; }
if(isset($s_g_7)){ $query .= "OR (generalf = '$s_g_7')"; }
if(isset($s_g_8)){ $query .= "OR (generalg = '$s_g_8')"; }
if(isset($s_g_9)){ $query .= "OR (generalh = '$s_g_9')"; }
if(isset($s_g_10)){ $query .= "OR (generali = '$s_g_10')"; }
if(isset($s_g_11)){ $query .= "OR (generalj = '$s_g_11')"; }
if(isset($s_g_12)){ $query .= "OR (generalk = '$s_g_12')"; }
if(isset($s_g_other)){ $query .= "OR (generall = '$s_g_other')"; }
if(isset($s_f_1)){ $query .= "OR (funda = '$s_f_1')"; }
if(isset($s_f_2)){ $query .= "OR (fundb = '$s_f_2')"; }
if(isset($s_f_3)){ $query .= "OR (fundc = '$s_f_3')"; }
if(isset($s_f_4)){ $query .= "OR (fundd = '$s_f_4')"; }
if(isset($s_f_5)){ $query .= "OR (funde = '$s_f_5')"; }
if(isset($s_f_6)){ $query .= "OR (fundf = '$s_f_6')"; }
if(isset($s_f_7)){ $query .= "OR (fundg = '$s_f_7')"; }
if(isset($s_f_8)){ $query .= "OR (fundh = '$s_f_8')"; }
if(isset($s_f_9)){ $query .= "OR (fundi = '$s_f_9')"; }
if(isset($s_f_10)){ $query .= "OR (fundj = '$s_f_10')"; }
if(isset($s_f_11)){ $query .= "OR (fundk = '$s_f_11')"; }
if(isset($s_f_12)){ $query .= "OR (fundl = '$s_f_12')"; }
if(isset($s_f_13)){ $query .= "OR (fundm = '$s_f_13')"; }
if(isset($s_f_other)){ $query .= "OR (fund = '$s_f_other')"; }
if(isset($s_a_1)){ $query .= "OR (acca = '$s_a_1')"; }
if(isset($s_a_2)){ $query .= "OR (accb = '$s_a_2')"; }
if(isset($s_a_3)){ $query .= "OR (accc = '$s_a_3')"; }
if(isset($s_a_4)){ $query .= "OR (accd = '$s_a_4')"; }
if(isset($s_a_5)){ $query .= "OR (acce = '$s_a_5')"; }
if(isset($s_a_6)){ $query .= "OR (accf = '$s_a_6')"; }
if(isset($s_a_7)){ $query .= "OR (accg = '$s_a_7')"; }
if(isset($s_a_8)){ $query .= "OR (acch = '$s_a_8')"; }
if(isset($s_a_9)){ $query .= "OR (acci = '$s_a_9')"; }
if(isset($s_a_10)){ $query .= "OR (accj = '$s_a_10')"; }
if(isset($s_a_11)){ $query .= "OR (acck = '$s_a_11')"; }
if(isset($s_a_other)){ $query .= "OR (accl = '$s_a_other')"; }
if(isset($s_p_1)){ $query .= "OR (puba = '$s_p_1')"; }
if(isset($s_p_2)){ $query .= "OR (pubb = '$s_p_2')"; }
if(isset($s_p_3)){ $query .= "OR (pubc = '$s_p_3')"; }
if(isset($s_p_4)){ $query .= "OR (pubd = '$s_p_4')"; }
if(isset($s_p_5)){ $query .= "OR (pube = '$s_p_5')"; }
if(isset($s_p_6)){ $query .= "OR (pubf = '$s_p_6')"; }
if(isset($s_p_7)){ $query .= "OR (pubg = '$s_p_7')"; }
if(isset($s_p_8)){ $query .= "OR (pubh = '$s_p_8')"; }
if(isset($s_p_9)){ $query .= "OR (pubi = '$s_p_9')"; }
if(isset($s_p_other)){ $query .= "OR (pubj = '$s_p_other')"; }
if(isset($s_i_1)){ $query .= "OR (ita = '$s_i_1')"; }
if(isset($s_i_2)){ $query .= "OR (itb = '$s_i_2')"; }
if(isset($s_i_3)){ $query .= "OR (itc = '$s_i_3')"; }
if(isset($s_i_4)){ $query .= "OR (itd = '$s_i_4')"; }
if(isset($s_i_5)){ $query .= "OR (ite = '$s_i_5')"; }
if(isset($s_i_6)){ $query .= "OR (itf = '$s_i_6')"; }
if(isset($s_i_7)){ $query .= "OR (itg = '$s_i_7')"; }
if(isset($s_i_8)){ $query .= "OR (ith = '$s_i_8')"; }
if(isset($s_i_other)){ $query .= "OR (iti = '$s_i_other')";
此代码工作正常,但如果同时检查Access和Crystal,我想根据这两个标准搜索用户,但它只按一个搜索。我知道我可以检查两个复选框的条件并在查询中添加AND。但是我如何为所有复选框执行此操作?
HTML
<form method="post" enctype="multipart/form-data" name="searchform" action="">
<tr>
<td align="left"><input value="1" <?php echo ($s_g_1=='1')?'checked':'' ?> id="s_g_1" name="s_g_1" type="checkbox"><label for="s_g_1"><span>Access</span></label></td>
<td align="left"><input value="1" <?php echo ($s_f_1=='1')?'checked':'' ?> id="s_f_1" name="s_f_1" type="checkbox"><label for="s_f_1"><span>ACT</span></label></td>
<td align="left"><input value="1" <?php echo ($s_a_1=='1')?'checked':'' ?> id="s_a_1" name="s_a_1" type="checkbox"><label for="s_a_1"><span>Blackbaud Financial Edge </span></label></td>
<td align="left"><input value="1" <?php echo ($s_p_1=='1')?'checked':'' ?> id="s_p_1" name="s_p_1" type="checkbox"><label for="s_p_1"><span>Corel Draw</span></label></td>
<td align="left"><input value="1" <?php echo ($s_i_1=='1')?'checked':'' ?> id="s_i_1" name="s_i_1" type="checkbox"><label for="s_i_1"><span>Mac</span></label></td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_2=='1')?'checked':'' ?> id="s_g_2" name="s_g_2" type="checkbox"><label for="s_g_2"><span>Crystal Reports </span></label></td>
<td align="left"><input value="1" <?php echo ($s_f_10=='1')?'checked':'' ?> id="s_f_10" name="s_f_10" type="checkbox"><label for="s_f_10"><span>Convio</span></label></td>
<td align="left"><input value="1" <?php echo ($s_a_2=='1')?'checked':'' ?> id="s_a_2" name="s_a_2" type="checkbox"><label for="s_a_2"><span>Fund EZ</span></label></td>
<td align="left"><input value="1" <?php echo ($s_p_8=='1')?'checked':'' ?> id="s_p_8" name="s_p_8" type="checkbox"><label for="s_p_8"><span>Dreamweaver</span></label></td>
<td align="left"><input value="1" <?php echo ($s_i_2=='1')?'checked':'' ?> id="s_i_2" name="s_i_2" type="checkbox"><label for="s_i_2"><span>PC</span></label></td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_3=='1')?'checked':'' ?> id="s_g_3" name="s_g_3" type="checkbox"><label for="s_g_3"><span>Excel</span></label></td>
<td align="left"><input value="1" <?php echo ($s_f_2=='1')?'checked':'' ?> id="s_f_2" name="s_f_2" type="checkbox"><label for="s_f_2"><span>Donor2</span></label></td>
<td align="left"><input value="1" <?php echo ($s_a_3=='1')?'checked':'' ?> id="s_a_3" name="s_a_3" type="checkbox"><label for="s_a_3"><span>Fundware</span></label></td>
<td align="left"><input value="1" <?php echo ($s_p_2=='1')?'checked':'' ?> id="s_p_2" name="s_p_2" type="checkbox"><label for="s_p_2"><span>Front Page</span></label></td>
<td align="left"><input value="1" <?php echo ($s_i_3=='1')?'checked':'' ?> id="s_i_3" name="s_i_3" type="checkbox"><label for="s_i_3"><span>Web</span></label></td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_4=='1')?'checked':'' ?> id="s_g_4" name="s_g_4" type="checkbox"><label for="s_g_4"><span>Filemaker Pro</span></label></td>
<td align="left"><input value="1" <?php echo ($s_f_3=='1')?'checked':'' ?> id="s_f_3" name="s_f_3" type="checkbox"><label for="s_f_3"><span>Donor Perfect</span></label></td>
<td align="left"><input value="1" <?php echo ($s_a_11=='1')?'checked':'' ?> id="s_a_11" name="s_a_11" type="checkbox"><label for="s_a_11"><span>Great Plains</span></label></td>
<td align="left"><input value="1" <?php echo ($s_p_3=='1')?'checked':'' ?> id="s_p_3" name="s_p_3" type="checkbox"><label for="s_p_3"><span>Illustrator</span></label></td>
<td align="left"><input value="1" <?php echo ($s_i_4=='1')?'checked':'' ?> id="s_i_4" name="s_i_4" type="checkbox"><label for="s_i_4"><span>Windows XP</span></label></td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_11=='1')?'checked':'' ?> id="s_g_11" name="s_g_11" type="checkbox"><label for="s_g_11"><span>HTML</span></label></td>
<td align="left"><input value="1" <?php echo ($s_f_11=='1')?'checked':'' ?> id="s_f_11" name="s_f_11" type="checkbox"><label for="s_f_11"><span>eTapestry</span></label></td>
<td align="left"><input value="1" <?php echo ($s_a_4=='1')?'checked':'' ?> id="s_a_4" name="s_a_4" type="checkbox"><label for="s_a_4"><span>MIP</span></label></td>
<td align="left"><input value="1" <?php echo ($s_p_9=='1')?'checked':'' ?> id="s_p_9" name="s_p_9" type="checkbox"><label for="s_p_9"><span>InDesign</span></label></td>
<td align="left"><input value="1" <?php echo ($s_i_5=='1')?'checked':'' ?> id="s_i_5" name="s_i_5" type="checkbox"><label for="s_i_5"><span>Help Desk</span></label></td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_6=='1')?'checked':'' ?> id="s_g_6" name="s_g_6" type="checkbox"><label for="s_g_6"><span>Lotus</span></label></td>
<td align="left"><input value="1" <?php echo ($s_f_4=='1')?'checked':'' ?> id="s_f_4" name="s_f_4" type="checkbox"><label for="s_f_4"><span>Fundmaster</span></label></td>
<td align="left"><input value="1" <?php echo ($s_a_5=='1')?'checked':'' ?> id="s_a_5" id="" name="s_a_5" type="checkbox"><label for="s_a_5"><span>OneWrite Plus</span></label></td>
<td align="left"><input value="1" <?php echo ($s_p_4=='1')?'checked':'' ?> id="s_p_4" name="s_p_4" type="checkbox"><label for="s_p_4"><span>Pagemaker</span></label></td>
<td align="left"><input value="1" <?php echo ($s_i_6=='1')?'checked':'' ?> id="s_i_6"name="s_i_6" type="checkbox"><label for="s_i_6"><span>Oracle</span></label></td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_7=='1')?'checked':'' ?> id="s_g_7" name="s_g_7" type="checkbox"><label for="s_g_7"><span>Outlook</span></label></td>
<td align="left"><input value="1" <?php echo ($s_f_5=='1')?'checked':'' ?> id="s_f_5" name="s_f_5" type="checkbox"><label for="s_f_5"><span>GiftmakerPRO </span></label></td>
<td align="left"><input value="1" <?php echo ($s_a_6=='1')?'checked':'' ?> id="s_a_6" name="s_a_6" type="checkbox"><label for="s_a_6"><span>Peachtree</span></label></td>
<td align="left"><input value="1" <?php echo ($s_p_5=='1')?'checked':'' ?> id="s_p_5" name="s_p_5" type="checkbox"><label for="s_p_5"><span>Photoshop</span></label></td>
<td align="left"><input value="1" <?php echo ($s_i_7=='1')?'checked':'' ?> id="s_i_7" name="s_i_7" type="checkbox"><label for="s_i_7"><span>Windows NT</span></label></td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_8=='1')?'checked':'' ?> id="s_g_8" name="s_g_8" type="checkbox"><label for="s_g_8"><span>Powerpoint</span></label></td>
<td align="left"><input value="1" <?php echo ($s_f_12=='1')?'checked':'' ?> id="s_f_12" name="s_f_12" type="checkbox"><label for="s_f_12"><span>GIFTS</span></label></td>
<td align="left"><input value="1" <?php echo ($s_a_7=='1')?'checked':'' ?> id="s_a_7" name="s_a_7" type="checkbox"><label for="s_a_7"><span>Quickbooks</td>
<td align="left"><input value="1" <?php echo ($s_p_6=='1')?'checked':'' ?> id="s_p_6" name="s_p_6" type="checkbox"><label for="s_p_6"><span>Quark</td>
<td align="left"><input value="1" <?php echo ($s_i_8=='1')?'checked':'' ?> id="s_i_8" name="s_i_8" type="checkbox"><label for="s_i_8"><span>Windows 2000</td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_12=='1')?'checked':'' ?> id="s_g_12" name="s_g_12" type="checkbox"><label for="s_g_12"><span>Salesforce</td>
<td align="left"><input value="1" <?php echo ($s_g_5=='1')?'checked':'' ?> id="s_g_5" name="s_g_5" type="checkbox"><label for="s_g_5"><span>iMIS</td>
<td align="left"><input value="1" <?php echo ($s_a_8=='1')?'checked':'' ?> id="s_a_8" name="s_a_8" type="checkbox"><label for="s_a_8"><span>Quicken</td>
<td align="left"><input value="1" <?php echo ($s_p_7=='1')?'checked':'' ?> id="s_p_7" name="s_p_7" type="checkbox"><label for="s_p_7"><span>MS Publisher </td>
<td align="left"><input value="1" <?php echo ($s_i_other=='1')?'checked':'' ?> id="s_i_other" name="s_i_other" type="checkbox"><label for="s_i_other"><span>Other</td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_9=='1')?'checked':'' ?> id="s_g_9" name="s_g_9" type="checkbox"><label for="s_g_9"><span>Word</td>
<td align="left"><input value="1" <?php echo ($s_f_6=='1')?'checked':'' ?> id="s_f_6" name="s_f_6" type="checkbox"><label for="s_f_6"><span>Millenium</td>
<td align="left"><input value="1" <?php echo ($s_a_9=='1')?'checked':'' ?> id="s_a_9" name="s_a_9" type="checkbox"><label for="s_a_9"><span>Real World</td>
<td align="left"><input value="1" <?php echo ($s_p_other=='1')?'checked':'' ?> id="s_p_other" name="s_p_other" type="checkbox"><label for="s_p_other"><span>Other</td>
<td align="left"> </td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_10=='1')?'checked':'' ?> id="s_g_10" name="s_g_10" type="checkbox"><label for="s_g_10"><span>Word Perfect</span></label></td>
<td align="left"><input value="1" <?php echo ($s_f_7=='1')?'checked':'' ?> id="s_f_7" name="s_f_7" type="checkbox"><label for="s_f_7"><span>Paradigm</span></label></td>
<td align="left"><input value="1" <?php echo ($s_a_10=='1')?'checked':'' ?> id="s_a_10" name="s_a_10" type="checkbox"><label for="s_a_10"><span>Solomon</span></label></td>
<td align="left"> </td>
<td align="left"> </td>
</tr>
<tr>
<td align="left"><input value="1" <?php echo ($s_g_other=='1')?'checked':'' ?> id="s_g_other" name="s_g_other" type="checkbox"><label for="s_g_other"><span>Other</span></label></td>
<td align="left"><input value="1" <?php echo ($s_f_8=='1')?'checked':'' ?> id="s_f_8" name="s_f_8" type="checkbox"><label for="s_f_8"><span>Pledgemaker</span></label></td>
<td align="left"><input value="1" <?php echo ($s_a_other=='1')?'checked':'' ?> id="s_a_other" name="s_a_other" type="checkbox"><label for="s_a_other"><span>Other</span></label></td>
<td align="left"> </td>
<td align="left"> </td>
</tr>
<tr>
<td align="left"> </td>
<td align="left"><input value="1" <?php echo ($s_f_9=='1')?'checked':'' ?> id="s_f_9" name="s_f_9" type="checkbox"><label for="s_f_9"><span>Raiser's Edge </span></label></td>
<td align="left"> </td>
<td align="left"> </td>
<td align="left"> </td>
</tr>
<tr>
<td align="left"> </td>
<td align="left"><input value="1" <?php echo ($s_f_other=='1')?'checked':'' ?> id="s_f_other" name="s_f_other" type="checkbox"><label for="s_f_other"><span>Other</span></label></td>
<td align="left"> </td>
<td align="left"> </td>
<td align="left"> </td>
</tr>
</tbody></table>
</td>
</tr>
</form>
答案 0 :(得分:0)
使用AND
子句代替OR
来搜索
if(isset($s_g_1)){ $query .= "AND (generala = '$s_g_1')"; }
if(isset($s_g_2)){ $query .= "AND (generalb = '$s_g_2')"; }
我还建议,使用循环检查所有这些。
答案 1 :(得分:0)
我认为你需要一个输入来选择类型&#34;和&#34;的搜索。或&#34;或&#34;
$query .= "SELECT * FROM wordpress_candidate
WHERE (city = '$cty')
OR (state = '$stt')";
if (searchType = 'AND')
{
$and_or = 'AND'
}
else
{
$and_or = 'OR'
}
if(isset($s_g_1) && ($s_g_1)) { $query .= $and_or + " (generala = '$s_g_1')"; }
if(isset($s_g_2) && ($s_g_2)) { $query .= $and_or + " (generalb = '$s_g_2')"; }
答案 2 :(得分:0)
据我所知,您只想过滤用户检查技能。如果未选中复选框,则无论用户是否具备该技能,都无关紧要;正确的吗?
在这里,您只过滤积极的技能:
if ($s_g_1) { $query .= " AND (generala = 1) "; }
if ($s_g_2) { $query .= " AND (generalb = 1) "; }
...
因此,如果选择“访问”和“水晶”,您将获得具有这两种技能(AND
)的所有用户,无论其他技能如何。
(我不会在if条件中与isset()
核对 - 我只想查询我的查询中的条件,如果$s_g_1
不是0
(=如果选中复选框) 。如果您不知道,是否定义了$s_g_1
,请使用!empty($s_g_1)
进行检查以避免NOTICE
。)