Chapter10 Search Record Using SELECT
Chapter10 Search Record Using SELECT
Simple search
Create a HTML form to receive the user’s request. In this example, we will be doing search
based on the first name, from the table employee of the database mycompanyhr.
</body>
</html>
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:1
Chapter 10: Search Record using SELECT
Create another file to receive the name entered by the user. The filename if you refer to the
action attribute in the form is simplesearch.php.
<html>
<head>
<title>Search by Name</title>
</head>
<body>
<?php
$searchName=$_GET['txtsearch'];
include ("connection.php");
<?php
while ($rekod=mysqli_fetch_array($qr)){//redo to other records
?>
<tr>
<td><?=$rekod['EMPNO']?></td>
<td><?=$rekod['FIRSTNAME']?></td>
<td><?=$rekod['LASTNAME']?></td>
<td><?=$rekod['WORKDEPT']?></td>
<td><?=$rekod['PHONENO']?></td>
</tr>
<?php
}//end of records
?>
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:2
Chapter 10: Search Record using SELECT
</table>
<?php
}//end if there are records
?>
</body>
</html>
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:3
Chapter 10: Search Record using SELECT
It’s much more user friendly if we use a single page for the user’s request and the record
listing.
<html>
<head>
<title>Listing a Result Set in a Table</title>
</head>
<body>
Search for name<br>
<?php 2
$searchName=$_GET['txtsearch'];
//this will execute if a name is inserted for search
if( $searchName!= NULL){ 3
//Include the connection details
include ("connection.php");
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:4
Chapter 10: Search Record using SELECT
<?php
while ($rekod=mysqli_fetch_array($qr)){//redo to other records
?>
<tr>
<td><?=$rekod['EMPNO']?></td>
<td><?=$rekod['FIRSTNAME']?></td>
<td><?=$rekod['LASTNAME']?></td>
<td><?=$rekod['WORKDEPT']?></td>
<td><?=$rekod['PHONENO']?></td>
</tr>
<?php
}//end of records
?>
</table>
<?php
}//end if there are records
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:5
Chapter 10: Search Record using SELECT
To provide a much easier searching facility for the user, we can prepare a multiple option to
choose. Instead of search by name only, user can choose search by their preferred field.
Create the form to receive user’s query. Prepare a text box and a combo box/dropdown list.
The drop down list will contain all criteria to choose.
<body>
Search for name<br>
</body>
</html>
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:6
Chapter 10: Search Record using SELECT
<body>
<?php
//fetch the search item
$searchitem=$_REQUEST['txtsearch'];
//fetch the search criteria
$searchcriteria=$_REQUEST['cmbfield'];
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:7
Chapter 10: Search Record using SELECT
?>
<b>Searching for <?=$searchitem?> using
<?=$searchcriteria?></b><br>
<table width="90%" border="1">
<tr align="center">
<td>Employee no.</td>
<td>First name</td>
<td>Last name</td>
<td>Department code</td>
<td>Phone no.</td>
</tr>
<?php
while ($rekod=mysqli_fetch_array($qr)){//redo to other records
?>
<tr>
<td><?=$rekod['EMPNO']?></td>
<td><?=$rekod['FIRSTNAME']?></td>
<td><?=$rekod['LASTNAME']?></td>
<td><?=$rekod['WORKDEPT']?></td>
<td><?=$rekod['PHONENO']?></td>
</tr>
<?php
}//end of records
?>
</table>
<?php
}//end if there are records
?>
</body>
</html>
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:8
Chapter 10: Search Record using SELECT
Search the employee table with the combination of first name, hired year and gender. Create
the first file to receive user’s request, filename formsearchcombinecriteria.php.
<html>
<head>
<title>Search by Combined Criteria </title>
</head>
<body>
Search for employee<br>
</body>
</html>
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:9
Chapter 10: Search Record using SELECT
This is the page to receive the search criteria and list the match records. The filename is
searchcombinecriteria.php.
<html>
<head>
<title>List Searched Records by Combined Criteria </title>
</head>
<body>
<?php
$firstname=$_REQUEST['txtfirstname'];
$hiredyear=$_REQUEST['cmbyearhired'];
$sex=$_REQUEST['rsex'];
<?php
while ($rekod=mysqli_fetch_array($qr)){//redo to other records
?>
<tr>
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:10
Chapter 10: Search Record using SELECT
<td><?=$rekod['EMPNO']?></td>
<td><?=$rekod['FIRSTNAME']?></td>
<td><?=$rekod['LASTNAME']?></td>
<td><?=$rekod['WORKDEPT']?></td>
<td><?=$rekod['PHONENO']?></td>
</tr>
<?php
}//end of records
?>
</table>
<?php
}//end if there are records
?>
</body>
</html>
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman – kerul.blogspot.com) Chapter 10:11