使用mySQL下拉搜索查询

时间:2015-02-05 14:39:14

标签: php mysql

我在搜索查询中添加下拉选项时遇到问题。我将下拉列表作为单选按钮正常工作,但是当我尝试将其更改为下拉列表时,查询只会提取所有记录。搜索栏和复选框仍然可以正常工作。

搜索页面:

<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
<?php require($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>

<?php include("../includes/header-home.php"); ?>
<div class="container">
  <div class="col=md-12">
    <p><strong>Search:</strong></p>
   <form name="form1" method="post" action="search_results.php">
    <p><input name="search" type="text" size="40" maxlength="50"/></p>
    <p><strong>Type:</strong></p>
     <input type="checkbox" name="type[]" value="1"> Medalist</p>

<select name="medal">
<option value="1">Federal Employee of the Year</option>
<option value="2">Career Achievement Medal</option>
<option value="3">Call to Service Medal</option>
<option value="4">Citizen Services Medal</option>
<option value="5">Homeland Security and Law Enforcement Medal</option>
<option value="6">Management Excellence Medal</option>
<option value="7">National Security and International Affairs Medal</option>
<option value="8">Science and Environment Medal</option>
</select>


    <p><input type="submit" name="submit" value="Search" /></p>
</form>
    </div></div>
<?php include($_SERVER['DOCUMENT_ROOT']."/includes/footer.php");?>

搜索结果页:

<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>

<?php
if (!isset ($_POST['search'])) {
    header("Location:admin.php");
}
$search_sqli="SELECT * FROM profiles WHERE 
    (first_name LIKE '%".$_POST ['search']."%' 
    OR last_name LIKE '%".$_POST ['search']."%' 
    OR first_name2 LIKE '%".$_POST ['search']."%' 
    OR last_name2 LIKE '%".$_POST ['search']."%' 
    OR last_name2 LIKE '%".$_POST ['search']."%' 
    OR city LIKE '%".$_POST ['search']."%' 
    OR agency LIKE '%".$_POST ['search']."%' 
    OR subcomponent LIKE '%".$_POST ['search']."%' 
    OR team_name LIKE '%".$_POST ['search']."%' 
    OR achievement LIKE '%".$_POST ['search']."%' 
    OR profile LIKE '%".$_POST ['search']."%')" 
. (isset($_POST['type']) && in_array('1', $_POST['type']) ? " AND medalist='1'" : "")
. (isset($_POST['FYE']) && in_array('1', $_POST['FYE']) ? " AND medal='1'" : "")
. (isset($_POST['CAM']) && in_array('2', $_POST['CAM']) ? " AND medal='2'" : "")
. (isset($_POST['C2S']) && in_array('3', $_POST['C2S']) ? " AND medal='3'" : "")
. (isset($_POST['CSM']) && in_array('4', $_POST['CSM']) ? " AND medal='4'" : "")
. (isset($_POST['HSLE']) && in_array('5', $_POST['HSLE']) ? " AND medal='5'" : "")
. (isset($_POST['MEM']) && in_array('6', $_POST['MEM']) ? " AND medal='6'" : "")
. (isset($_POST['NSIA']) && in_array('7', $_POST['NSIA']) ? " AND medal='7'" : "")
. (isset($_POST['SEM']) && in_array('8', $_POST['SEM']) ? " AND medal='8'" : "");


$search_query=mysqli_query($connection, $search_sqli);
if (mysqli_num_rows($search_query) !=0)  {
$search_rs=mysqli_fetch_assoc($search_query);
}

?>

<?php include("../includes/header-home.php"); ?>
<div class="container">
  <div class="col=md-12">
     <p>Search:</p>
    <form name="form1" method="post" action="search_results.php">
    <input name="search" type="text" size="40" maxlength="50"/>
    <input type="submit" name="submit" value="Search" />

    </form>
   <br />
    <p><strong>Search Results:</strong></p>
  <?php if (mysqli_num_rows($search_query) !=0) {
     do  {
         ?>
    <p><ul>
    <li><a href="view_profile.php?profile=<?php echo urlencode($search_rs["id"]); ?>"><?php echo $search_rs['first_name']; ?> <?php echo $search_rs['last_name']; ?> <?php echo $search_rs['first_name2']; ?> <?php echo $search_rs['last_name2']; ?></a></li></ul></p>     

<?php } while ($search_rs=mysqli_fetch_assoc($search_query));

  } else {
      echo "No results found";
  }
  ?>

  <p> <a class="btn btn-default" href="search.php" role="button">Back to search</a></p>
    </div></div>

<?php include($_SERVER['DOCUMENT_ROOT']."/includes/footer.php");?>

1 个答案:

答案 0 :(得分:0)

您的select的名称为medal,因此您应该在PHP中使用它。例如:

. (isset($_POST['medal']) && $_POST['medal'] == '1') ? " AND medal='1'" : "")
. (isset($_POST['medal']) && $_POST['medal'] == '2') ? " AND medal='2'" : "")
. (isset($_POST['medal']) && $_POST['medal'] == '3') ? " AND medal='3'" : "")
... etc

有一些方法可以简化这一点,例如:

. (isset($_POST['medal']) && (int)$_POST['medal'] > 0) ? " AND medal='".(int)$_POST['medal']."'" : "")

将替换上面的所有8行。


另请注意,您的FYECAM等变量看起来像是一个数组(例如name='FYE[]'),而您的medal变量不是。在PHP中检索这些内容时,请确保注意使用的内容。