I don't understand what's the problem with the code. It is supposed to work. There is data in table but the search is still not producing any results. The Search bar remaining still and no changes before or, after entering any data in the search bar.
Here is the code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search Example</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="Search" />
<input type="submit" value="submit" />
</form>
<?php
$con = mysqli_connect("localhost","root","") or die("Could not connect");
mysqli_select_db($con ,"project") or die(mysqli_error());
if(isset($_POST['search']))
{
$search =$_POST['search']; // searchq contains characters which is typed
in the search
$search = preg_replace("#[^0-9a-z]#i","",$search); //filtering the
conditions */
$query = mysqli_query($con, "SELECT * FROM admin WHERE name LIKE
'%$search%'");
//most important condition line for the search
$count = mysqli_num_rows($query); // To count the selected Rows
if($count==0)
{
echo"<h2>"."No Data Found"."</h2>";
}
else
{
while($row = mysqli_fetch_array($query))
{
echo "<tr>".
"<td>".$row['username'] ."</td>".
"<td>".$row['password'] ."</td>".
"</tr>";
}
}
}
?>
</body>
</html>
Database name is project and table name is admin
with Id
, username
and password
as columns.