我想为多个字段进行搜索,如果填写任何字段,则应相应地执行搜索。 下面是我用于多个字段搜索的查询
$select_us =("SELECT * FROM profiles WHERE (";
if($_GET['school']!='')
{
$select_us.="SELECT * FROM profiles WHERE profile_id IN(select distinct school from education where school Like '$level%') ";
}
if($_GET['status']!='')
{
$select_us.="SELECT * FROM profiles WHERE status Like '$level%'";
}
if($_GET['officee']!='')
{
$select_us.="SELECT * FROM profiles WHERE Office Like '$Office%'";
}
select_us.=")");
我有一个HTML表单,如下所示
<form action="" method="get">
<input name="keyword" style="width:70%;" class="wpcf7-form-control wpcf7-text" placeholder="Keyword" aria-invalid="false" type="text">
<input name="last_name" style="width:70%;" class="wpcf7-form-control wpcf7-text" aria-invalid="false" placeholder="Last Name" type="text" onKeyUp="showResultf(this.value)"><div id="livesearchf" style=" transition: border linear .2s, box-shadow linear .2s; box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); color:#000; width:31%; font-size:16px; margin-top:-10px; float:left; background:#e9f2f7; overflow:hidden; max-height:400px; z-index:88; position:absolute; padding:0px 10px; line-height:22px; " ></div>
<input name="firs_name" style="width:70%;" placeholder="First Name" class="wpcf7-form-control wpcf7-text" aria-invalid="false" type="text" onKeyUp="showResultfirst(this.value)"><div id="livesearchfirst" style="color:#000; width:31%; margin-top:-10px; float:left; background:#e9f2f7; overflow:hidden; max-height:400px; z-index:88; position:absolute; padding:0px 10px; line-height:22px; " ></div><br />
<select style="width:70%;" name="status" class="wpcf7-form-control wpcf7-select" aria-invalid="false"><option value="">Select a Level</option><?php $select_stauts =("SELECT distinct status FROM profiles where status!='' order by status asc ");
$fetch_data_status = $conn->query($select_stauts);
while($fetch_rows_status = $fetch_data_status->fetch_assoc())
{
$statuss=$fetch_rows_status['status']; ?><option value="<?php echo $statuss ; ?>"><?php echo $statuss ; ?></option><?php } ?></select><br />
<select style="width:70%;" name="officee" class="wpcf7-form-control wpcf7-select" aria-invalid="false"><option value="">Select an Office</option><?php $select_offcie =("SELECT distinct Office FROM profiles where Office!='' order by Office asc ");
$fetch_data_office = $conn->query($select_offcie);
while($fetch_rows_office = $fetch_data_office->fetch_assoc())
{
$office=$fetch_rows_office['Office']; ?><option value="<?php echo $office ; ?>"> <?php echo $office ; ?></option><?php } ?></select>
<select style="width:70%;" name="service_area" class="wpcf7-form-control wpcf7-select" aria-invalid="false"><option value="">Select Service Area</option> <?php $select_city =("SELECT distinct city FROM profiles where city!='' order by city asc ");
$fetch_data_city = $conn->query($select_city);
while($fetch_rows_city = $fetch_data_city->fetch_assoc())
{
$city=$fetch_rows_city['city']; ?><option value="<?php echo $city ; ?>"><?php echo $city ; ?></option><?php } ?></select><br />
<select name="baradmissionn" class="wpcf7-form-control wpcf7-select" aria- invalid="false" style="width:70%;"><option value="">Select Bar Admission</option> <?php $select_bar =("SELECT distinct admitted FROM profiles order by admitted asc ");
$fetch_data_bar = $conn->query($select_bar);
while($fetch_rows_bar = $fetch_data_bar->fetch_assoc())
{
$admitted=$fetch_rows_bar['admitted']; ?><option value="<?php echo $admitted ; ?>"><?php echo $admitted ; ?></option><?php } ?></select>
<select name="school" style="width:70%;" class="wpcf7-form-control wpcf7-select" aria-invalid="false"><option value="">Select a School</option><?php $select_school =("SELECT distinct school FROM education where school!='' order by school asc ");
$fetch_data_school = $conn->query($select_school);
while($fetch_rows_school = $fetch_data_school->fetch_assoc())
{
$schooll=$fetch_rows_school['school']; ?>
<option value="<?php echo $schooll ; ?>"><?php echo $schooll ; ?></option> <?php } ?></select> <br />
<input value="Search" class="wpcf7-form-control wpcf7-submit" name="search" type="submit" style="margin-left:60%;"></form>
请帮我查询一下。感谢。
答案 0 :(得分:1)
你不能这样写查询。
在下面进行查询,如
if($_GET['school']!='')
{
$condition .=' and profile_id in (select distinct school from education where school Like "$level%")'
}
// make other condtion like this
$select_us ="SELECT * FROM profiles WHERE 1=1 ". $condition;
它会起作用