I'm creating a simple search by first name. the field is indexed as FULLTEXT.
I want to be able to type in a name in the search box and have it match the input name and display results with only those names.
I'm learning php so this is a student question.
Thanks!
Here is the code:
Code: Select all
<?php include('connectdb.php');?>
<?php
// force script errors and warnings to show during production opnly
error_reporting(E_ALL);
ini_set('display_errors', '1');
// initialize the searh output variable
$search_output="";
if(isset($_POST['searchQuery']) && $_POST ['searchQuery'] !=""){
// filter the search uqery user input
$searchQuery = preg_replace('#[^a-z 0-9?]#i', '', $_POST['searchQuery']);
$sqlCommand = "(SELECT filedName AS title FROM some_tbl WHERE firstName LIKE '%$searchQuery%') UNION (SELECT filedName AS title FROM some_tbl WHERE firstName LIKE '%$searchQuery%')";
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$search_output .= "$count results for $searchQuery! $sqlCommand";
while ($row = mysql_fetch_array($query)){
$location = $row['firstName'];
$search_output .= "Results: $location";
}
} else {
$search_output = "no Results Found for $searchQuery! $sqlCommand";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<strong>Search For:</strong> <br><br>
<input name="searchQuery" type="text" size="80" maxlength="88" value="City, State" class="search-input">
<input type="submit" value="Search">
</form>