-1

Below is my script that is executed when a user enters information into a search bar and hits the submit button. I keep getting the error "Warning: mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement". The number of variables and values is equal, yet I keep getting this error. Any help would be greatly appreciated, thank you!

I have also included an image of my current database table here

  include_once 'dbh-inc.php';

    if (isset($_POST['submit-search'])) {

      $search = $_POST['search'];

      $sql = "SELECT * FROM animals WHERE animal_animaltype LIKE '%?%' OR animal_breed LIKE '%?%' OR descGallery LIKE '%?%'";

      $stmt = mysqli_stmt_init($conn);
      if (!mysqli_stmt_prepare($stmt, $sql)) {
            echo "SQL error";
        } else {
            mysqli_stmt_bind_param($stmt, "sss", $search, $search, $search);
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
        $resultCount = mysqli_num_rows($result);

        if ($resultCount > 0) {
          echo "There are ".$resultCount." results for your search";
          while ($row = mysqli_fetch_assoc($result)) {
              echo '<a href="#">
              <div style="background-image: url(img/gallery/'.$row["imgFullNameGallery"].');"></div>
              <h3>'.$row["animal_name"].'</h3>
              <h3>'.$row["animal_breed"].'</h3>
              <p>'.$row["descGallery"].'</p>
              <br>
              <h1>'.'Current owner: '.$row["animal_owner"].'</h1>  
            </a>';
          }
        }
        }
    }
jd618
  • 7
  • 2
  • preperation of LIKE statements work a bit different: https://stackoverflow.com/questions/18527659/php-mysqli-prepared-statement-like – Jeff Dec 11 '18 at 01:11

1 Answers1

1

Remove the '%%' from the query and add the %% to the variable $search:

$search = '%' . $_POST['search'] . '%';

$sql = "SELECT * FROM animals 
          WHERE   animal_animaltype LIKE ? 
               OR animal_breed LIKE ? 
               OR descGallery LIKE ?";
Jeff
  • 6,895
  • 1
  • 15
  • 33
  • 1
    Note `concat` also could be used, `animal_animaltype LIKE concat('%', ?, '%')` or even `concat(animal_animaltype, ' ', animal_breed, ' ', descGallery) LIKE concat('%', ?, '%')` then only need 1 binding – user3783243 Dec 11 '18 at 02:14