Rebekah
Rebekah

Reputation: 77

How to create a search using PHP, mysqli and a html form

I want to create a form which allows the user to type in a search and have it pick up the right values from a database and display them, for some reason I can't get my query to work it just displays "could not search"

Here is my php code

 <?php

  include "connect.php";

  $output = '';

  if(isset($_POST['search'])) {
    $search = $_POST['search'];
    $search = preg_replace("#[^0-9a-z]i#","", $search);

    $query = mysqli_query("SELECT * FROM house WHERE town LIKE '%$search%'") or die ("Could not search");
    $count = mysqli_num_rows($query);
    
    if($count == 0){
      $output = "There was no search results!";

    }else{

      while ($row = mysqli_fetch_array($query)) {

        $town = $row ['town'];
        $street = $row ['street'];
        $bedrooms = $row ['bedrooms'];
        $bathroom = $row ['bathrooms'];

        $output .='<div> '.$town.''.$street.''.$bedrooms.''.$bathrooms.'</div>';

      }

    }
  }

  ?>

Here is my form

 <form action ="home.php" method = "post">
  
          <input name="search" type="text" size="30" placeholder="Belfast"/>

          <input type="submit" value="Search"/>

          </form> 

          <?php print ("$output");?>

Upvotes: 3

Views: 30612

Answers (3)

butter bee
butter bee

Reputation: 1

Try to remove the space between if($count==0) it will start working!!

if($count==0){
      $output = "There was no search results!";}

Upvotes: -1

reyven
reyven

Reputation: 1

$query = mysqli_query("SELECT * FROM house WHERE town LIKE '%$search%'") or die ("Could not search");

$result = mysqli_query($connection,$query);

$count = mysqli_num_rows($result);

The $connection is the variable declared in your connect.php to connect to your database .

You should have out the $result before the $count.

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

You're not connecting to your DB in your query:

$query = mysqli_query("SELECT
                      ^ missing connection variable

there is no connection variable (unknown what you are using to connect with)

$query = mysqli_query($connection, "SELECT ...
                      ^^^^^^^^^^^^

From the manual http://php.net/manual/en/mysqli.query.php

Object oriented style mixed mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

Procedural style mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

figuring you are using mysqli_ to connect with. If you're using mysql_ or PDO to connect with, that won't work. Those different MySQL APIs do not intermix with each other.

Plus, instead of or die ("Could not search") do or die(mysqli_error($connection)) to catch any errors, if any.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Example mysqli connection:

$connection = mysqli_connect("myhost","myuser","mypassw","mybd") 
                or die("Error " . mysqli_error($connection)); 

For more information, visit:

Upvotes: 2

Related Questions