Ask Question

Menu


How to create search option or feature using PHP


In this example i am going to explain how to create search option or feature using PHP and MySQLi .In this example we search the students data by roll no.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>Search Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form class="form-inline" method="post" action="search.php">
<input type="text" name="roll_no" class="form-control" placeholder="Search roll no..">
<button type="submit" name="save" class="btn btn-primary">Search</button>
</form>
</div>
</body>
</html>

search.php

<?php
error_reporting(0);
$conn = mysqli_connect("localhost","root","","student");
if(count($_POST)>0) {
$roll_no=$_POST[roll_no];
$result = mysqli_query($conn,"SELECT * FROM user_data where roll_no='$roll_no' ");
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Retrive data</title>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<td>Name</td>
<td>Email</td>
<td>Roll No</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><?php echo $row["roll_no"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>


Buy Template