search based on 2 criteria (search-name-job.php)

This PHP script is used to cater search if two criteria are used. The first criteria is the employee’s FULLNAME and the second one is the employee’s JOB. The form will receive the values, and the SQL is customized based on the user’s input.

<html>
<head>
<title> Search for employee's name</title>
</head>
<body>
<form method="get" action="">
Search for employee's name<br>
Enter a name <input type="text" name="txtname"><br>
Enter the job <input type="text" name="txtjob"><br>
<input type="submit" value="Search">
</form>
<?php
/*database connection information
database server location=localhost
database username=root
password to username=NULL
database name=mycompanyhr
*/
$db=mysqli_connect("localhost","root","","mycompanyhr");
if($db==true){
    echo "Connection succeed!!!<br>";
}
else{
    echo "Connection fail!!!<br>";
}
//extract the name and job from the form
$employeename=$_GET['txtname'];
$job=$_GET['txtjob'];
/*develop the SQL command with condition
FULLNAME and JOB based on the value in the form*/
$query="select *
        from employee
  where FULLNAME LIKE '%$employeename%' 
  and JOB LIKE '%$job%'";
  
//execute the SQL command
$rs=mysqli_query($db,$query);
if($rs==false){
  //if SQL command fail, display the SQL errors
  echo ("Query cannot be executed!<br>");
  echo ("SQL Error : ".mysqli_error($db));
}
//if NO record found
if(mysqli_num_rows($rs)==0){
  echo "No record found...";
}
//if records found, display in table
else{
?>
<table border="1"> 
<tr>     
    <td>Employee Number</td>
        <td>Name</td>
        <td>Department</td>
        <td>Phone</td>
        <td>Job</td>
</tr>
<?php
    while($rekod=mysqli_fetch_array($rs)){
         echo (" <tr><td>".$rekod['EMPNO']."</td>");
         echo (" <td> ".$rekod['FULLNAME']."</td>");
         echo (" <td> ".$rekod['DEPT']."</td>");
         echo (" <td> ".$rekod['PHONENO']."</td>");
         echo (" <td> ".$rekod['JOB']."</td></tr>");
    }
?>
</table>
<?php 
}//end else available records
?>
</body>
</html>


Output



search-name-job

Popular Posts