Click here to Skip to main content
15,886,755 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php
// Include database config file
require_once 'dbConfig.php';

// If search form is submitted
$searchKeyword = $whrSQL = '';
if(isset($_POST['searchSubmit'])){
	$searchKeyword = $_POST['keyword'];
	if(!empty($searchKeyword)){
		// SQL query to filter records based on the search term


	$whrSQL = "WHERE (title LIKE '%".' '.$searchKeyword."%' OR description LIKE '%".$searchKeyword.' '."%')";




	}
}
// Get matched records from the database
$result = $db->query("SELECT * FROM posts $whrSQL ORDER BY id DESC");

// Highlight words in text
function highlightWords($text, $word){
	$text = preg_replace('#'. preg_quote($word) .'#i', '\\0', $text);
	return $text;
}

?>

<!DOCTYPE html>
<html lang="en-US">
<head>
<title> Codeat21 - PHP Highlight Keywords in Search Results with MySQL </title>
<meta charset="utf-8">

<!-- Stylesheet file -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
	<div class="row">
		<h2>Posts List (<?php echo $result->num_rows; ?>) </h2>
		<!-- Search form -->
		<form method="post">
			<div class="input-group">
				<input type="text" name="keyword" class="form-control" value="<?php echo $searchKeyword; ?>" placeholder="Search by keyword..." >
					<div class="input-group-append">
						<input type="submit" name="searchSubmit" class="btn btn-outline-secondary btn-colors" value="Search">
						<a href="index.php" class="btn btn-outline-secondary btn-colors2">Reset</a>
					 </div>
			</div>
		</form>

		<!-- Search results -->
		<?php
			if($result->num_rows > 0){
				while($row = $result->fetch_assoc()){
					$title = !empty($searchKeyword)?highlightWords($row['title'], $searchKeyword):$row['title'];
					$description = !empty($searchKeyword)?highlightWords($row['description'], $searchKeyword):$row['description'];
		?>
		<div class="list-item">
			<h4><?php echo $title; ?></h4>
			<p><?php echo $description; ?></p>
		</div>
		<?php } }else{ ?>
			<div class="list-item">
				<p>No post(s) found...</p>
			</div>
		<?php } ?>


    </div>
</div>
</body>
</html>



it searchs everyything i do not want this . i want only search words

https://i.stack.imgur.com/dhHCe.png[^]

What I have tried:

i tried that
$x = " ".$searchKeyword;
$y = $searchKeyword." ";

	$whrSQL = "WHERE (title LIKE '%".' '.$x."%' OR description LIKE '%".$x.' '."%')";


bu it doesn't work
Posted
Updated 28-Jul-21 3:02am
Comments
Richard Deeming 29-Jul-21 4:56am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
PHP: SQL Injection - Manual[^]

1 solution

The LIKE function is a pattern search so you need to try some of the other operators; see SQL Wildcard Characters[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900