我正在尝试创建搜索字段

时间:2019-12-05 18:13:25

标签: php html

我正在尝试在PHPHTML中创建一个搜索字段。我的代码没有显示任何错误消息,但是它没有回显任何内容。我的搜索页面空白。

这是我的PHP代码:

<?php
  require './includes/databaseHandler.php';

  if(isset($_POST['submit-search'])){
    $search = mysqli_real_escape_string($con, $_POST['search']);
    $location = mysqli_real_escape_string($con, $_POST['location']);
    $sql = "SELECT * FROM business WHERE BusinessName LIKE '%$search%' OR Address LIKE '%$search%'";
    $result = mysqli_query($con, $sql);
    $QueryResult = mysqli_num_rows($result);

    if($QueryResult > 0){
      while ($row = mysqli_fetch_assoc($result)){
        $url = $row['BusinessName']."<br/>";
        echo "<a href='Businesspage.php?id={$row['id']}'>" . $url . "</a>";
        echo $row['Address']."<br/>";
        echo $row['Phone']."<br/> <hr>";
      }
    }else{
      echo "There are no result matching your search";
    }
  }
?>

以及搜索表单中的HTML代码:

<form action="searchindex.php" method="POST">
  <input id="form-control" class="form-control" type="text" placeholder="What are you looking for?" name="search"><i id="form-control" class="fa fa-map-marker" aria-hidden="true"></i>
  <input id="form-control" class="form-control" type="text" placeholder="Jabi, Abuja?" name="location">
  <button class="btn btn-default" type="submit-search"><i class="fa fa-search"></i></button>
</form>

2 个答案:

答案 0 :(得分:2)

这是错误的:

<button class="btn btn-default" type="submit-search">

它必须是:

<button class="btn btn-default" type="submit" name="submit-search">

答案 1 :(得分:0)

您必须在按钮部分中使用名称标签,但您忘记了。 并且按钮type应该是submit而不是submit-search

使用此代码。

<button class="btn btn-default" name="submit-search" type="submit"><i class="fa fa-search"></i></button>