0

I have a search bar on my site. When I click on the search button I am getting the error above. I am not an advanced coder so I do not know how to fix it. I have searched online, there are solutions for this problem but they are not working for me. This is for a project that is due in a few days so I would appreciate any help I can get. It's also my first post on here so go easy on me please. Thank You

csearch.php

<?php
/**
*Performs a search
*
* This class is used to perform search function in a MySQL database
*
*/
class search {
  /**
  * MySQLi connection
  *@access private
  *@var object
  */
  private $mysqli;

  /**
  * Constructor
  *
  * This sets up the class
  */

  public function __construct(){
    // Connect to the database and store in $mysqli property_exists
    $this->connect();
   }
  /**
  * Database connection
  *
  * This connects to the database
  */
  private function connect() {
    $this->mysqli = new mysqli( 'localhost', 'root', '', 'games');
    }

  /**
  * Search routine
  *
  * Performs a search
  *
  * @param string $search_term The search term
  *
  *
  */
  public function search($search_term) {
    // Sanatize the search term to prevent injection attacks
    $sanitized = $this->mysqli->real_escape_string($search_term);

    // Run the query
    $query = $this->mysqli->query("
    SELECT title
    FROM games
    WHERE title LIKE '%($sanitized)%'
    OR body LIKE '%($sanitized)%'
    ");

  // Check results
  if ( ! $query->num_rows ) {
    return false;
    }

// Loop and fetch objects
while( $row = $query->fetch_object() ) {
  $rows[] = $row;
 }
return $search_results;
}
?>

search.php

<?php

$search_results= "";

//check if search data was submitted
if ( isset( $_GET['s'] ) ) {

  // Include the search class
  require_once( dirname( __FILE__ ) . '/csearch.php' );

  // Instantiate a new instance of the search class
  $search = new search();

  // Store search term into a variable
  $search_term = $_GET['s'];

  // Send the search term to the search class and store the result
  $search_results = $search->search($search_term);

}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta hhtp-equiv="X-UA-Compatible" content="IE-edge">
  <title>VGDB | SEARCH</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="../vgdb/css/main.css">
  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-
awesome.min.css" rel="stylesheet" integrity="sha384-
wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" 
crossorigin="anonymous">
  <link href='https://fonts.googleapis.com/css?family=Hind:600,400' 
rel='stylesheet'>
</head>
<body>
<?php include 'header.php';?>

<div class="container">
  <div class="col-md-5 col-md-offset-1">
    <div class="row">
      <div class="col-md-6">
    <div class="search">
      <h1>50,000 games and counting...</h1>
      <form action="" method="get">
        <input type="search" name="s" placeholder="Search for games..." 
results="5" value="<?php echo $search_term; ?>">
        <input type="submit" value="Search">
      </div>
    </div>
  </div>
</div>
</div>

</form>
<?php if ( $search_results ) : ?>
  <div class="results-count">
    <p><?php echo $search_results['count']; ?> results found</p>
  </div>
  <div class="results-table">
    <?php foreach ( $search_results['results'] as $search_result) : ?>
      <div class="result">
        <p><?php echo $search_result->title; ?></p>
      </div>
    <?php endforeach; ?>
  </div>
<?php endif; ?>

</body>
</html>
<?php include 'footer.php';?>
SyedNaqvi
  • 11
  • 1
  • 1
  • 1

1 Answers1

2

The function public function search($search_term) closes on line 66. You need to add another } to close class search

This is a result of poor formatting. Please make sure to format your code neatly so that minor mistakes like this can be avoided. :)

Sometimes it is the smallest thing that causes your application to crash and the smallest things can be hard to find sometimes...

CAllen
  • 856
  • 5
  • 14
  • Hi, thank you for your response. I added in the } however now I am getting the following error Parse error: syntax error, unexpected 'public' (T_PUBLIC) in C:\xampp\htdocs\vgdb\csearch.php on line 43. Any idea? – SyedNaqvi Apr 21 '17 at 20:58
  • No problem and I don't see any error why you would get that but for now try to commenting the `connect()` function and see if it still shows. If not then try changing `public function search` to `function search`. Also be sure to check if your connection is successful `if (mysqli_connect_errno()) { exit('Connect failed: ' , mysqli_connect_error());}` – CAllen Apr 21 '17 at 21:22