用多个搜索框在php mysql中搜索

时间:2016-01-14 12:26:00

标签: php mysql

我想用多个搜索框创建搜索。我已经在本教程https://www.youtube.com/watch?v=2XuxFi85GTw的帮助下创建了。所以我把它作为2个文本框进行搜索。这就是我所做的:



<?php
if (isset($_POST['SearchTime']))
{
  $ValueToSearch = $_POST['ValueToSearchTime'];
  $query = "SELECT `id`, `name`, `address`, `contact`, `email`, `date`, `time`, `ahc`, `chc`, `cottage`, `total` FROM `reserve`  WHERE CONCAT (`time`) LIKE '%".$ValueToSearch."%' ORDER BY id DESC";
  $search_result = filterTable($query);
}
elseif (isset($_POST['ValueToSearchTime']) == "") {
  $query = "SELECT id, name, address, contact, email, date, time, ahc, chc, total, cottage FROM reserve ORDER BY id DESC";
  $search_result = filterTable($query);
}

elseif (isset($_POST['SearchCottage']))
{
  $ValueToSearch = $_POST['ValueToSearchCottage'];
  $query = "SELECT `id`, `name`, `address`, `contact`, `email`, `date`, `time`, `ahc`, `chc`, `cottage`, `total` FROM `reserve`  WHERE CONCAT (`cottage`) LIKE '%".$ValueToSearch."%' ORDER BY id DESC";
  $search_result = filterTable($query);
}
elseif (isset($_POST['ValueToSearchCottage']) == "") {
  $query = "SELECT id, name, address, contact, email, date, time, ahc, chc, total, cottage FROM reserve ORDER BY id DESC";
  $search_result = filterTable($query);
}

function filterTable($query)
{
  $connect = mysqli_connect("localhost","root","","resort");
  $filter_Result = mysqli_query($connect,$query);
  return $filter_Result;
}

?>
<!DOCTYPE html>
<html>
<head>
  <title>Search Table</title>
  <link rel="stylesheet" href="lib/table.css" media="screen" title="no title" charset="utf-8">
  <link rel="stylesheet" href="lib/style.css" media="screen" title="no title" charset="utf-8">

</head>
<body>
<form action="index.php" method="post">
  <div class="container">
    <div class="TableGenerator">
      <table>
        <tr>
          <td>No.</td>
          <td>Name</td>
          <td>Address</td>
          <td>Contact</td>
          <td>Email Address</td>
          <td>Date</td>
          <td>
            Time<br/>
            <input type="text" name="ValueToSearchTime" class="txt" placeholder="Search Time"/>
            <input type="submit" name="SearchTime" value=">>"/>
          </td>
          <td>HeadCount <br/> <i>Adult</i></td>
          <td>HeadCount <br/> <i>Child</i></td>
          <td>Total<br/>Amount</td>
          <td>
            Cottage No<br/>
            <input type="text" name="ValueToSearchCottage" class="txt" placeholder="Search Cottage"/>
            <input type="submit" name="SearchCottage" value=">>"/>
          </td>
        </tr>
        <?php include ("includes/row.php"); ?>
        <?php while ($row = mysqli_fetch_array($search_result) ): ?>
        <tr>
          <td><?php echo $row['id']; ?></td>
          <td><?php echo $row['name']; ?></td>
          <td><?php echo $row['address']; ?></td>
          <td><?php echo $row['contact']; ?></td>
          <td><?php echo $row['email']; ?></td>
          <td><?php echo $row['date']; ?></td>
          <td><?php echo $row['time']; ?></td>
          <td><?php echo $row['ahc']; ?></td>
          <td><?php echo $row['chc']; ?></td>
          <td><?php echo $row['total']; ?></td>
          <td><?php echo $row['cottage']; ?></td>
        </tr>
        <?php endwhile;?>
      </table>
    </div>
  </div>
</form>
</body>
</html>
&#13;
&#13;
&#13;

但是当我在两个搜索框中输入内容时,我想同时过滤它。请帮忙!

1 个答案:

答案 0 :(得分:2)

您可以使用关键字AND

SELECT * FROM reserve WHERE firstcondition AND secondcondition