I am currently doing a web application which requires pulling out of data from the database. I am still a novice in the programming industry, and is still seeking help from my colleagues and of course forum sites like phpdn. I have an existing code which my friend provided me. I have already done some modifications with the code. I have a problem though with the code, it executes database query upon loading of the page. I do understand how the code works, however, I am not able to modify the code to disallow the execution of query upon loading of page. Here is the code:
Code: Select all
<?PHP
include("dbconnection.php");
$query = "SELECT * FROM records";
if(isset($_POST["btnSearch"]))
{
$query .= " WHERE last_name LIKE '%".$_POST["search"]."%' OR first_name LIKE '%".$_POST["search"]."%'OR territory LIKE '%".$_POST["search"]."%'OR job_title LIKE '%".$_POST["search"]."%'OR title LIKE '%".$_POST["search"]."%'OR employer LIKE '%".$_POST["search"]."%' " ;
}
$result = mysql_query($query, $connection) or die(mysql_error());
?>Here's the whole code:
Code: Select all
<link href="add_client.css" rel="stylesheet" type="text/css">
<?PHP
include("dbconnection.php");
$query = "SELECT * FROM records";
if(isset($_POST["btnSearch"]))
{
$query .= " WHERE last_name LIKE '%".$_POST["search"]."%' OR first_name LIKE '%".$_POST["search"]."%'OR territory LIKE '%".$_POST["search"]."%'OR job_title LIKE '%".$_POST["search"]."%'OR title LIKE '%".$_POST["search"]."%'OR employer LIKE '%".$_POST["search"]."%' " ;
}
$result = mysql_query($query, $connection) or die(mysql_error());
?>
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><table width="760" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="199" align="center" valign="top"><a href="login.html"><img src="asia.gif" alt="" width="152" height="58" border="0" /></a> <script type="text/javascript" src="menu.js"></script></td>
<td width="176" align="right" valign="bottom"><a href="main.php"><img src="Home.jpg" width="104" height="20" border="0"/></a></td>
<td width="130" align="right" valign="bottom"><img src="View.jpg" width="104" height="20" border="0"/></td>
<td width="146" align="right" valign="bottom"><a href="add_client.php"><img src="Add.jpg" width="104" height="20" border="0"/></a></td>
<td width="109" align="right" valign="bottom"> </td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="760" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="200" height="3" bgcolor="#1B1C78"><img src="images/topspacerblue.gif" alt="" width="1" height="3" /></td>
<td width="560" bgcolor="#0076CC"><img src="images/topspacerlblue.gif" alt="" width="1" height="3" /></td>
</tr>
</table></td>
</tr>
<tr>
<td height="553" align="center" valign="top" bgcolor="#F3FAFE"><br />
<form name="form" action="view_client.php" method="post">
<table width="351" border="0">
<tr>
<td width="137" align="left" valign="middle">SEARCH RECORD:</td>
<td width="144" align="center" valign="middle"><input type="text" name="search" /></td>
<td width="56" align="left" valign="middle"><input type="submit" name="btnSearch" value="Search" /></td>
</tr>
</table>
<br />
<table width="680" border="0" cellpadding="3" cellspacing="1" bordercolor="38619E" >
<tr>
<th width="100" align="center" bgcolor="#E0E8F3">Territory</th>
<th width="110" align="center" bgcolor="#E0E8F3">Employer</th>
<th width="110" align="center" bgcolor="#E0E8F3">Job Title</th>
<th width="50" align="center" bgcolor="#E0E8F3">Title</th>
<th width="110" align="center" bgcolor="#E0E8F3">First Name</th>
<th width="110" align="center" bgcolor="#E0E8F3">Last Name</th>
<th width="70" align="center" valign="middle" bgcolor="#E0E8F3"> </th>
</tr>
<?php
if($result)
{
for($i=0; $i<mysql_num_rows($result); $i++)
{
$id = trim(mysql_result($result, $i, "id"));
$territory = trim(mysql_result($result, $i, "territory"));
$employer = trim(mysql_result($result, $i, "employer"));
$job_title = trim(mysql_result($result, $i, "job_title"));
$title = trim(mysql_result($result, $i, "title"));
$first_name = trim(mysql_result($result, $i, "first_name"));
$last_name = trim(mysql_result($result, $i, "last_name"));
echo "<tr>";
echo "<td>".$territory."</td>";
echo "<td>".$employer."</td>";
echo "<td>".$job_title."</td>";
echo "<td>".$title."</td>";
echo "<td>".$first_name."</td>";
echo "<td>".$last_name."</td>";
echo "<td><a href='admin_edit.php?id=".$id."'>edit</a> | <a href='admin_delete.php?id=".$id."'>del</a> </td>";
echo "</tr>";
}
}
?>
</table>
<br />
</form>
<p> </p></td>
</tr>
<tr>
<td height="38"><table width="760" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="200" height="35" align="center" bgcolor="#1B1C78" class=white><img src="images/topspacerblue.gif" alt="" width="1" height="3" /> <a href="disclaimer.html"><font color="#FFFFFF">Legal Disclaimer</font></a> </td>
<td width="560" align="center" bgcolor="#0076CC" class=white><img src="images/topspacerlblue.gif" alt="" width="1" height="3" /> Copyright © 2006 - 2010 Limited. All rights reserved.
</td>
</tr>
</table></td>
</tr>
</table>Immediate response is well appreciated. Thank you very much!