Jump to content
Larry Ullman's Book Forums

Recommended Posts


I have built a forum for my own website thanks to the forum example in this book in chapter 17. Now I want the capability to search through posts and show the results of the search. I modified the search.php script included in the course files. I tested it and it doesn't work properly, the $_GET['terms'] doesn't get set when I type in search terms and click search. I tested it by echoing out 'hello' after the isset($_GET['terms']) . I don't know why it isn't set though, in the address bar it is written
search.php?terms=test&submit=Submit



<?php # Script 17.8 - search.php
// This page displays and handles a search form.

// Include the HTML header:


// Show the search form:
echo '<form action="search.php" method="get" accept-charset="utf-8">
<p><em>Search </em>: <input name="terms" type="text" size="30" maxlength="60" ';

// Check for existing value:
if (isset($_GET['terms'])) {
echo 'value="' . htmlspecialchars($_GET['terms']) . '" ';
}

// Complete the form:
echo '/><input name="submit" type="submit" value="' . $words['submit'] . '" /></p></form>';

if (isset($_GET['terms'])) { // Handle the form.
echo 'hello';
// Clean the terms:
$terms = mysqli_real_escape_string($dbc, htmlentities(strip_tags($_GET['terms'])));

// Run the query...
$q = "SELECT message FROM posts WHERE MATCH (message) AGAINST ('test' IN BOOLEAN MODE)";
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) > 0) {
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
echo '<h2>Search Results</h2>';
echo '<p>' . $row['message'] . '</p>';
} else {
echo '<p>No results found.</p>';
}

}


?>

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...