I am trying to create a simple search bar that allows the user to search using a user id to return a specific user.
I need to include SQL injection protection.
currently the page loads a blank screen but when I refresh the page I get the "No results found" response.
I have been reading a lot of posts about search bars but nothing is helping me.
here is the code:
<html>
<head>
<title></title>
</head>
<body>
<form action="search.php" method="POST">
<input type="text" name="search" />
<input type="submit" value="Search" />
</form>
</body>
<?php
//search.php
include("DbConnect.php");
$search = $_POST['search'];
$safesearch = mysqli_real_escape_string($search);
$Query = "SELECT user_id
FROM users
WHERE user_id = '$safesearch'";
$Result = mysqli_query($DB,$Query);
$NumResults = mysqli_num_rows($Result);
if ($NumResults==1)
{
echo "<h1>results: ".$Result."</h1>";
}else{
echo "<h1>No Results found</h1>";
}
?>