Hello, I have a php script with 3 textboxes that is supposed to search for and retrieve records using the following criteria:
Policy Number, First Name and Last Name.
The code is as follows:
search_page.php
<table width="1076" align="center" bgcolor="#eee">
<tr>
<td height="10" valign="top">
<p>
<div align="center">
<form id="form1" name="form1" method="post" action="search_page.php">
Policy No.:
<input name="policyNum" type="text" id="policyNum" size="20" value="<?php echo stripcslashes($_REQUEST["policyNum"]); ?>" />
Surname:
<input name="surname" type="text" id="surname" size="20" value="<?php echo stripcslashes($_REQUEST["surname"]); ?>"/>
First Name:
<input name="name" type="text" id="name" size="20" value="<?php echo stripcslashes($_REQUEST["name"]); ?>"/>
<input type="submit" name="button" id="button" value="Search Filter" />
</label>
<a href="search_page.php">
reset</a>
</form>
<br /><br />
<table width="700" border="1" cellspacing="0" cellpadding="4">
<tr>
<td width="90" bgcolor="#CCCCCC"><strong>Policy No.</strong></td>
<td width="95" bgcolor="#CCCCCC"><strong>Name</strong></td>
<td width="159" bgcolor="#CCCCCC"><strong>Surname</strong></td>
</tr>
<?php
if ($_REQUEST["policyNum"]<>'') {
$search_policy = " AND (policyNumber LIKE '%".mysql_real_escape_string($_REQUEST["policyNum"])."%')";
}
if ($_REQUEST["surname"]<>'') {
$search_surname = " AND (lastName LIKE '%".mysql_real_escape_string($_REQUEST["surname"])."%')";
}
if ($_REQUEST["name"]<>'') {
$search_name = " AND (firstName LIKE '%".mysql_real_escape_string($_REQUEST["name"])."%')";
}
else {
$sql = "SELECT * FROM tblclients WHERE clientID > 0".$search_policy.$search_surname.$search_name;
}
$sql_result = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr>
<td><?php echo $row["policyNumber"]; ?></td>
<td><?php echo $row["firstName"]; ?></td>
<td><?php echo $row["lastName"]; ?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php
}
?>
</table>
and the config.php
<?php
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("underwrite", $con);
?>
My problem is that i can retrieve the records using Policy No. and Surname textboxes, but i cannot do so using First Name textbox and it displays the error "Query was empty". I have all the data in my database and have double checked the names of the database fields but to no avail. What could be wrong?