Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling same page for database query - refresh issue 1

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
HI all,

On every page in my site is a search box named txtSearch. The form calls search.php. In search.php, the value of txtSearch is tested against entries in the database. That works fine.

But here is the problem. Let's say I start on the home page (index.php). I enter apples for a search term. The search.php page appears with any "apples" returned from the database. Now I am still on the search page. I enter "oranges". The search page still returns apples.

How can a form on a page, call itself for the action, and make the page rerun it's code (in this case querying the database for oranges).

I thought perhaps I have to clear the cache. So I tried this code...

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

... but it did nothing.

Any ideas?? Help!

Thanks,
KB
 
Can you show us some code i.e. your search.php script?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Sure....

.... regular html stuff here

<form id="form2" name="form2" method="post" action="search.php?item=None">
<label><span class="text2">Search</span></label>
<input type="text" name="txtSearch" size="12" />
</form>

... more html

<?php
$db_host = "00.0.000.0";
$db_name = "zzzzzzz";
$db_user ="yyyyyyy";
$db_pass = "xxxxxxx";
$conn=mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name, $conn);
If ($REQUEST['item']=="None"){
$searchwords=explode(" ", $_REQUEST['txtSearch']);
} else {
$searchwords=$_REQUEST['item'];
}
$words=count($searchwords);
$found=0;
$query="Select InternalKey, SetName, SetLongDesc, RetailPrice, Manufacturer, Thumbnail, Pix, Category, SubCategory, Keywords From Inventory";
$result = mysql_query($query,$conn) or die("a wrench in the works");
while ($row=mysql_fetch_array($result, MYSQL_NUM)) {
for ($i=0; $i<$words; $i++) {

if (stristr( $row[9], $searchwords[$i]) || stristr( $row[2], $searchwords[$i])) {
$found=$found+1;
echo '<tr>';
Switch ($row[4]) {
case "aaa":
echo '<td valign="top"><img src="images/aaa/' . $row[5] . '"width="100"/><br>';
break;
case "bbb":
echo '<td valign="top"><img src="images/bbb/' . $row[5] . '"width="100"/><br>';
break;
}

echo $row[1] . '<BR>' . '$' . $row[3] . '<BR>' . substr($row[2],0,50) . '...';
echo '</td>';
echo '</tr>';
}
}
}
echo ' </table>';
if ($found==0) {
echo 'No items found';
}

 
Some observations:
1) For searches, I generally set the form's method attribute to "get" as this means the search terms are submitted via the URL - making it easy to bookmark a set of search results.

2) I'm not sure I understand why you're using "item=None" in the form's action attribute?

3) You can code up your pages in the following manner:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">

<head>
  <title>Home</title>
</head>

<body>

<form id="form2" name="form2" method="get" action="search.php">
  <label><span class="text2">Search</span></label>
  <input type="text" name="txtSearch" size="12" />
</form>

<h1>Index Page</h1>
<p>Blah, blah, blah</p>

</body>

</html>
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">

<head>
  <title>Search</title>
</head>

<body>

<form id="form2" name="form2" method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>">
  <label><span class="text2">Search</span></label>
  <input type="text" name="txtSearch" size="12" />
</form>

<?php
  if (isset($_REQUEST['txtSearch'])) {
    // process search terms in this if statement
    echo '<p>Search term: ' . $_REQUEST['txtSearch'] . '</p>';
  }
?>
</body>

</html>

4) You're not really utilising the power of database queries. You should apply the search terms to the query using a WHERE clause, rather than returning all rows from the database and manually looping through each row looking for each of the search terms.

Example:
Code:
$query="
SELECT InternalKey, SetName, SetLongDesc, Keywords
FROM Inventory
WHERE (SetLongDesc LIKE '%" . mysql_real_escape_string($_REQUEST['txtSearch']) . "%') OR (Keywords LIKE '%" . mysql_real_escape_string($_REQUEST['txtSearch']) . "%')";
For more info about the WHERE clause, check out:

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Hi, thanks,

1) Get or Post - a matter of preference I suppose. Perhaps people out browsing or shopping on the web like to save the searches. Never considered that. I would think it would make for a cluttered list of favorites?

2) item=None is because there is another page on the site that offers prefixed searches - such as for "desk", "chair" etc. For those I am using a query string, such as item=desk. This in turn caused me to add the item=None when using the freeform search. The code I pasted above tests for that.

3) I didn't use a where clause because of the "explode" on the keywords. In other words someone might type in "leather chair with armrests". The process needs to return any items that have leather OR chair OR armrests. I need to go lookup your suggestion of using mysql_real_escape_string($_REQUEST['txtSearch']). Does mysql_real_escape_string accomplish the explode of the array and get every value treated on its own in the query? That would be way cool!
 
if you need to search in the manner you suggest, then i recommend moving to a full text index. OR queries are quickly going to get out of hand otherwise.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top