How to Search Employee data from Database using PHP | PHP Gurukul

Search Employee data from Database

In this tutorial, we will learn how to search employee records on the basis of their name, email, mobile number, and employee id in PHP from the database.

In this example, we will guide you a step-by-step procedure to search employee records.

Search employee data in PHP and MySQL will look as follows.

How-Search-Employee-data-from-Database-using-PHP

Step1: First create a database with name “betdb” where employee data is stored.

Step2: Second is to create a table with the name “tblempdata” and insert the employee data.

Table Structure

1234567891011CREATE TABLE`tblempdata`(  `ID`int(5)NOTNULL,  `EmployeeName`varchar(200)DEFAULTNULL,  `Department`varchar(200)DEFAULTNULL,  `Email`varchar(200)DEFAULTNULL,  `MobileNumber`varchar(200)DEFAULTNULL,  `EmpID`varchar(200)DEFAULTNULL,  `JoiningDate`date DEFAULTcurrent_timestamp(),  `PostingDate`timestamp NULLDEFAULTcurrent_timestamp())ENGINE=InnoDB DEFAULTCHARSET=latin1;

Step3: Create a database connection file and save this file with the name “config.php”. Below is the code of the database connection.

123456789<?php$con=mysqli_connect(“localhost”,”root”,””,”betdb”);// Check connectionif(mysqli_connect_errno()){echo”Failed to connect to MySQL: “.mysqli_connect_error();}?>

Step4: Create a form with one field which is “search”.

123456789101112131415161718192021<div class=”col-xs-12″><h3 style=”padding-left: 100px;”>Search employee data from database on the basis of Name,Email,Mobile Number andEmployee ID</h3><hr><form name=”bwdatesdata”action=””method=”post”action=””><table width=”100%”height=”117″  border=”0″><tr><th width=”27%”height=”63″scope=”row”>Search:</th><td width=”73%”><input id=”searchdata”type=”text”name=”searchdata”required=”true”class=”form-control”></td>  </tr><tr>    <th width=”27%”height=”63″scope=”row”></th>    <td width=”73%”>    <button class=”btn-primary btn”type=”submit”name=”search”>Submit</button>  </tr></table></form></div>

Step 5: PHP code for fetching data from the database on the basis of the search.

Blog : https://phpgurukul.com/how-to-search-employee-data-from-database-using-php

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950<div class=”row”><div class=”col-xs-12″><?phpif(isset($_POST[‘search’])){$sdata=$_POST[‘searchdata’];  ?><h3 style=”padding-left: 100px;color:blue”>Result against”<?php echo $sdata;?>”keyword</h3><hr><div class=”row”><table class=”table table-bordered”width=”100%”  border=”0″style=”padding-left:40px”><thead>                                    <tr>                                        <th scope=”col”>S.NO</th>                                        <th scope=”col”>Employee Name</th>                                        <th scope=”col”>Department</th>                                        <th scope=”col”>Mobile Number</th>                                        <th scope=”col”>Email</th>                                        <th scope=”col”>Employee ID</th>                                        <th scope=”col”>JoiningDate</th>                                    </tr>                                </thead><?php$ret=mysqli_query($con,”select * from  tblempdata where EmployeeName like ‘%$sdata%’ || Email like ‘%$sdata%’ || MobileNumber like ‘%$sdata%’ || EmpID like ‘%$sdata%'”);$num=mysqli_num_rows($ret);if($num>0){$cnt=1;while($row=mysqli_fetch_array($ret)){?><tbody><tr data-expanded=”true”><td><?phpecho$cnt;?></td>                                <td><?php  echo$row[‘EmployeeName’];?></td>                  <td><?php  echo$row[‘Department’];?></td>                  <td><?php  echo$row[‘MobileNumber’];?></td>                  <td><?php  echo$row[‘Email’];?></td>                  <td><?php  echo$row[‘EmpID’];?></td>                  <td><?php  echo$row[‘JoiningDate’];?></td>                </tr>                 <?php$cnt=$cnt+1;}}else{?>  <tr>    <td colspan=”8″>Norecordfoundagainstthissearch</td>  </tr><?php}}?></tbody></table></div></div>

Here is the full code that we have written for this tutorial:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110<?phprequire_once(“config.php”);?><!DOCTYPE html><html lang=”en”><head><meta http-equiv=”content-type”content=”text/html; charset=UTF-8″><meta charset=”utf-8″><title>PHP GURUKUL|DEMO</title><meta name=”generator”content=”Bootply”/><meta name=”viewport”content=”width=device-width, initial-scale=1, maximum-scale=1″><link href=”css/bootstrap.min.css”rel=”stylesheet”><!–[ifltIE9]><script src=”//html5shim.googlecode.com/svn/trunk/html5.js”></script><![endif]–><link href=”css/styles.css”rel=”stylesheet”>   </head><body><nav class=”navbar navbar-default navbar-fixed-top”role=”navigation”><div class=”navbar-header”>  <h4 style=”padding-left: 100px;padding-top: 20px;”>PHP GURUKUL|Programming Blog</h4></div></nav><div class=”container-fluid”>  <!–center–>  <div class=”col-sm-8″>    <div class=”row”>      <div class=”col-xs-12″>        <h3 style=”padding-left: 100px;”>Search employee data from database on the basis of Name,Email,Mobile Number andEmployee ID</h3><hr><form name=”bwdatesdata”action=””method=”post”action=””>  <table width=”100%”height=”117″  border=”0″><tr>    <th width=”27%”height=”63″scope=”row”>Search:</th>    <td width=”73%”><input id=”searchdata”type=”text”name=”searchdata”required=”true”class=”form-control”>     </td>  </tr><tr>    <th width=”27%”height=”63″scope=”row”></th>    <td width=”73%”>     <button class=”btn-primary btn”type=”submit”name=”search”>Submit</button>  </tr></table>     </form>      </div>    </div>    <hr>      <div class=”row”>      <div class=”col-xs-12″><?phpif(isset($_POST[‘search’])){$sdata=$_POST[‘searchdata’];  ?><h3 style=”padding-left: 100px;color:blue”>Result against”<?php echo $sdata;?>”keyword</h3><hr><div class=”row”><table class=”table table-bordered”width=”100%”  border=”0″style=”padding-left:40px”><thead>                      <tr>                      <th scope=”col”>S.NO</th>                      <th scope=”col”>Employee Name</th>                      <th scope=”col”>Department</th>                      <th scope=”col”>Mobile Number</th>                      <th scope=”col”>Email</th>                      <th scope=”col”>Employee ID</th>                       <th scope=”col”>JoiningDate</th>                       </tr>                          </thead><?php$ret=mysqli_query($con,”select * from  tblempdata where EmployeeName like ‘%$sdata%’ || Email like ‘%$sdata%’ || MobileNumber like ‘%$sdata%’ || EmpID like ‘%$sdata%'”);$num=mysqli_num_rows($ret);if($num>0){$cnt=1;while($row=mysqli_fetch_array($ret)){?><tbody><tr data-expanded=”true”>            <td><?phpecho$cnt;?></td>                  <td><?php  echo$row[‘EmployeeName’];?></td>                  <td><?php  echo$row[‘Department’];?></td>                  <td><?php  echo$row[‘MobileNumber’];?></td>                  <td><?php  echo$row[‘Email’];?></td>                  <td><?php  echo$row[‘EmpID’];?></td>                  <td><?php  echo$row[‘JoiningDate’];?></td>                </tr>                 <?php$cnt=$cnt+1;}}else{?>  <tr>    <td colspan=”8″>Norecordfoundagainstthissearch</td>  </tr><?php}}?></tbody></table></div>      </div>    </div>  </div><!–/center–>  <hr></div><!–/container-fluid–><!–scriptreferences–><script src=”//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js”></script><script src=”js/bootstrap.min.js”></script></body></html>

Download Script

Download Script Now


How to Run the Script

1. Download the zip file

2. Extract the file and copy searchempdata folder

3.Paste inside root directory(for xampp xampp/htdocs, for wamp wamp/www, for lamp var/www/HTML)

4.Open PHPMyAdmin (http://localhost/phpmyadmin)

5. Create a database with the name betdb

6. Import betdb.sql file(given inside the zip package in the SQL file folder)

7. Run the script http://localhost/searchempdata

Website : https://phpgurukul.com

Leave a Reply

Your email address will not be published. Required fields are marked *