无法进行多字段搜索

时间:2015-02-03 15:27:12

标签: 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"/> <input type="submit" name="submit" value="Search" /></p>
<p><strong>Type:</strong></p>
    <form name="form1" method="post" action="search_results.php">
<p><input type="checkbox" name="keywords[]" value="1"> Finalist <input type="submit" name="submit" value="Search" /></p>

    </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']."%'";
$search_query=mysqli_query($connection, $search_sqli);
if (mysqli_num_rows($search_query) !=0)  {
$search_rs=mysqli_fetch_assoc($search_query);
}

else{
    $sql="SELECT * FROM profiles WHERE finalist = '1'";
   $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 :(得分:1)

您需要创建一个表单来保存所有输入标记,并且只需要一个提交输入:

<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>
    <p><input type="checkbox" name="keywords[]" value="1"> Finalist</p>
    <p><input type="submit" name="submit" value="Search" /></p>
</form>

如果您创建了不同的表单,则只会将包含您使用的提交按钮的数据与请求一起发送。

之后,您还需要将两个查询合并为一个,如下所示:

$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['keywords']) && in_array('1', $_POST['keywords']) ? " AND finalist='1'" : "");

如果选中了复选框,则会在查询末尾添加AND finalist='1'