Skip to content
Advertisement

why PHP search page show blank box with no result?

I’m trying to do a search bar here. I have done my database, filled it with data, connected it to my php file, did the search bar code everything is fine but when i tried to search for a word its showing me blank box with zero result This is my db connect code

<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "root";
$dbName = "product_list";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

and this is search page code

<?php
include '/Applications/MAMP/htdocs/Pharmacy Project/db.php';
?>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.html'; ?>
<style>
  <?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.css';
  ?>
</style>
<?php ?>
<style>
  <?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/SearchBar/searchbar.css'; ?>
</style>
<style>
  <?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css' ?>
</style>

<div class="result-container">
  <?php
  if (isset($_POST['Search'])) {
    $search = mysqli_real_escape_string($conn, $_POST['search']);
    $sql = "SELECT * FROM product_list WHERE a_Name LIKE '%$search%' OR Pharmacy LIKE '%$search%'";
    $result = mysqli_query($conn, $sql);
    $queryresult = mysqli_num_rows($result);

    echo 'There are' . $queryresult . 'results!';

    if ($queryresult > 0) {
      while ($row = mysqli_fetch_assoc($result)) {
        echo "<div class='result-box'>
      <h3>" . $row['a_Name'] . "</h3>
      <p>" . $row['Price'] . "</p>
      <p>" . $row['Pharmacy'] . "</p>
      </div>";
      }
    } else {
      echo "Sorry No Result :(!";
    }
  }
  ?>
</div>

this is my form page

<?php
include '/Applications/MAMP/htdocs/Pharmacy Project/db.php';
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <link rel="stylesheet" type="text/css" href="/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Home Screen</title>
</head>

<body>
  <?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.html'; ?>
  <style>
    <?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.css';
    ?>
  </style>
  <?php ?>
  <style>
    <?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/SearchBar/searchbar.css'; ?>
  </style>
  <style>
    <?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css' ?>
  </style>
  <form action="search.php" method="POST">
    <div id="searchbar">
      <input type="text" name="search" placeholder="Search">
      <button id='sbutton'>Search</button>
    </div>
  </form>

  <h2 class="allresult">All Result</h2>
  <div class="result-container">
    <?php
    $sql = "SELECT * FROM Product";
    $result = mysqli_query($conn, $sql);
    $queryresults = mysqli_num_rows($result);
    if ($queryresults > 0) {
      while ($row = mysqli_fetch_assoc($result)) {
        echo "<div class='result-box'>
      <h3>" . $row['a_Name'] . "</h3>
      <p>" . $row['Price'] . "</p>
      <p>" . $row['Pharmacy'] . "</p>
      </div>";
      }
    }
    ?>
  </div>
</body>

</html> 

I’m new to PHP

Advertisement

Answer

Your search field name is search in lowercase but you are checking for Search in $_POST which is capitalized. Make sure they match and it will fix the issue

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement