How to create Search Engine in PHP for website

HTML code for Search-Box

<div class="search-box" >
<form action="search.php" method="get">
     <input id="text-style" type="text" size="25"  name="search" placeholder="search this site..." required>
     <input id="button" type="submit" name="submit" value="Search">
</form>
</div>

CSS For Search-box


 #text-style
{
  height:16px; 
  border-radius:4px;
  margin-top: 2px;
  padding:2px;
  border:0px solid #dbdbdb;
 background:#dedede;
}
#button
{
  border: 2px solid #207cca;
  background:#207cca;
  border-radius: 2px;
  margin-top: 3px;
  width: 60px;
  height:28px;
  cursor: pointer;
  font-weight: bold;
  font-size: 14px;
  color:#fafafa;
}
#button:hover  
{
background-color:#fafafa;
color:#207cca;
}

PHP code for search engine

<?PHP
$servername = "localhost";
$username = "root";
$password = "";
$db="my database";

$con = new mysqli($servername, $username, $password,$db);

if ($con->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if(isset($_GET ['submit'] )){

$search_id = $_GET['search'];

$query="SELECT * FROM posts WHERE post_title  like '%$search_id%'";

$result=mysqli_query($con,$query);

while ($row=mysqli_fetch_array ($result)){
$post_id=$row ['post_id'];
$post_title=$row ['post_title'];
$post_image=$row ['post_image'];
$post_content=substr($row ['post_content'],0,1000);
}
?>
<h2> 
     <a href="pages.php ?id = <?php echo $post_id;?>">

           <?php echo $post_title;?> 
      </a> 
</h2>

<center>
<img src="images/<?php echo $post_image;?>" width= "100" height= "100"/>

</center>

<p align=justify>
<?php echo $post_content; ?> 
</p>

<?php } ?>

No comments:

Post a Comment