Simple search – result appears in the same page

This PHP code is a simple search example. It needs the user to enter a name and the PHP script will to the searching by the name provided by the user in the text box.



<form name='search' method='get' action=''>

Insert the firstname

<input type='text' name='txtname'>

<input type='submit' value='search name'>

</form>

<?php

//database connect

$db=mysqli_connect("localhost","root","","mycompanyhr");



//checking database connection

if ($db==false) {

echo "Connect failed: ". mysqli_connect_error($db);

exit();

}

else {

echo "Connection successful";

}

$n=$_GET['txtname'];

$sql="select EMPNO, FIRSTNAME, LASTNAME, WORKDEPT,

PHONENO

from employee

where FIRSTNAME like '%$n%' ";



$rs=mysqli_query($db,$sql);

if($rs==false){

echo ("Query cannot be executed!<br>");

echo ("SQL Error : ".mysqli_error($db));

}

else{

echo "<table border=1>";

echo "<tr>";

echo "<td>Employee no</td>";

echo "<td>First name</td>";

echo "<td>Last name</td>";

echo "<td>Department</td>";

echo "<td>Phone ext</td>";

echo "</tr>";



while($rekod=mysqli_fetch_array($rs)){

echo "<tr>";

echo "<td>".$rekod['EMPNO']."</td>";

echo "<td>".$rekod['FIRSTNAME']."</td>";

echo "<td>".$rekod['LASTNAME']."</td>";

echo "<td>".$rekod['WORKDEPT']."</td>";

echo "<td>".$rekod['PHONENO']."</td>";

echo "</tr>";

}

}

echo "</table>";

?>



Output



simplesearch

Popular Posts

WorldConferences.net