Simple PHP code to search a record

image

<html>
<head>
<title>Search by Name</title>
</head>
<body>
Search for name<br>
<form action="" method="get" name="formsearch">
Enter a name <input name="txtsearch" type="text">
<input name="btnsearch" type="submit" value="Search">
</form>
 
<?php
//to create the bridge
$db=mysqli_connect("localhost","root","","mycompanyhr");
 
//connection failed
if($db==false){//connection error
    echo("Connection failed : ". 
            mysqli_connect_error($db));
    exit();
}
//u are connected to db
else{
    echo("Connected to database <hr>");
}
 
$n=$_GET['txtsearch'];
//sql command to retrieve records
$sql="select * from employee
        where FIRSTNAME like '%$n%'";
 
//execute the sql command
$qr=mysqli_query($db, $sql);
 
 
//if there's any SQL error
if($qr==false){
    echo ("Query cannot be executed!<br>");
    echo ("SQL Error : ".mysqli_error($db));
}
//no SQL error, so display data
else{
    echo "<table border=1>";
    while($rekod=mysqli_fetch_array($qr)){
        echo "<tr>";
        echo "<td>".$rekod['EMPNO']."</td>";
        echo "<td>".$rekod['FIRSTNAME']."</td>";
        echo "<td>".$rekod['LASTNAME']."</td>";
        echo "<td>".$rekod['PHONENO']."</td>";
        echo "<td>".$rekod['JOB']."</td>";
        echo "<tr>";
    }
    echo "</table>";
}
?>
</body>
</html>

Popular Posts