Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6254661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:13:09+00:00 2026-05-24T14:13:09+00:00

I am creating a simple search feature that queries a user input string for

  • 0

I am creating a simple search feature that queries a user input string for a matches in a products catalog that is held within a table in mysql.

This is my code for this search form:

<form name="search" method="post" action="'.$_SERVER['PHP_SELF'].'">
    <div>
    Search For Product: <input type="text" name="find" style="background: #F4F4F4; font-family: "Lucida Console", Monaco, monospace;" />
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Search" />
    </div>
</form>

Here is the code that processes this string:

//This is only displayed if they have submitted the form 
if ($searching == "yes") 
{ 
$pageContent = '
<h2>Place your order</h2>
<p>Your order contains '.count($_SESSION['order']).' items. (<a href="?orders">View your order</a>)</p>
<form name="search" method="post" action="'.$_SERVER['PHP_SELF'].'">
    <div>
    Search For Product: <input type="text" name="find" style="background: #F4F4F4; font-family: "Lucida Console", Monaco, monospace;" />
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Search" />
    </div>
</form>
<br />
';


//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
$pageContent .= '<p>You forgot to enter a search term</p>';
echo $head1 . $pageDetails . $head2 . $header . $menu . $belowMenu . $content . $pageContent . $footer . $pageScripts; 
exit;
} 

// Otherwise we connect to our Database 
$bccConn   = mysql_connect($bccHost, $bccUser, $bccPass) or exit(mysql_error());
             mysql_select_db($bccDB, $bccConn) or exit(mysql_error());

// We preform a bit of filtering 

$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 
$keywords_array = explode(' ', $find);

//Now we search for our search term, in the field the user specified 
$dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE '%" 
   . implode("%' AND upper(`desc`) LIKE '%", $keywords_array) 
   . "%'";

$data = mysql_query($dataQuery) or die(mysql_error());

//And we remind them what they searched for
$pageContent .= '
<p><b>Searched For:</b>  ' . $find . '</p>
';

$tempVar = 0;
//And we display the results 
while ($result = mysql_fetch_array($data)) {
$prId = $result['id'];
$prRefCode = $result['refCode'];
$prDesc = $result['desc'];
$prPack = $result['pack'];
$prMeasure = $result['measure'];
$prQuantity = $result['quantity'];
$prDeptCode = $result['deptCode'];
$prTaxable = $result['taxable'];
$prPrice1 = $result['price1'];
$prPrice2 = $result['price2'];
$prCrdCode = $result['crdCode'];
$prCost1 = $result['cost1'];
$prCost2 = $result['cost2'];

if ($tempVar == 0) {
$pageContent .= '
<p>All prices are inclusive of VAT</p>
<table border="1">
<thead>
    <tr>
        <th>Stock Code</th>
        <th>Description</th>
        <th>Packsize</th>
        <th>Price</th>
        <th>In-Stock?</th>
        <th>Quantity</th>
        <th>Submit</th>
    </tr>
</thead>
<tbody>
';
}
if ($tempVar == mysql_num_rows($data)) {
    $pageContent .= '
</tbody>
</table>
';
}

    $pageContent .= '
<tr>
    <td>' . $prId . '</td>
    <td>' . $prDesc . '</td>
    <td>' . $prPack . 'x' . $prSize . ' ' . $prMeasure . '</td>
    <td>R' . $prPrice1 . '</td>
';
    if (empty($prQuantity)) {
        $pageContent .= '
<td>No</td>
';
    } else {
        $pageContent .= '
<td>Yes</td>
';
    }
    $pageContent .= '
    <form action="" method="post">
    <td>            
            <div>
                <input type="text" name="quantity" size ="2" value ="1" style="background: #F4F4F4; font-family: "Lucida Console", Monaco, monospace;"/>
            </div>            
    </td>
    <td>
            <div>
                <input type="hidden" name="id" value="' . $prId . '" />
                <input type="submit" name="action" value="Order" />
            </div>            
    </td>
    </form>           
 </tr>
 ';
    $tempVar ++;
 }
 $pageContent .= '
 </tbody>
</table>
';

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches = mysql_num_rows($data);
if ($anymatches == 0) {
    $pageContent .= '
<p>Sorry, but we can not find an entry to match your query</p>
';
}
}

Here is the code that deals with the event of a user clicking the “Order” button:

if (!isset($_SESSION['order']))
{
$_SESSION['order'] = array();
}

if (!isset($_SESSION['quantity']))
{
$_SESSION['quantity'] = array();
}

if (isset($_POST['action']) and $_POST['action'] == 'Order')
{
// Add item to the end of the $_SESSION['order'] array
$_SESSION['order'][$_POST['id']] = $_POST['id'];
$_SESSION['quantity'][$_POST['id']] = $_POST['quantity'];
header('Location: .');
exit();
}

What I would like to happen, is when the user clicks on order, after the header redirect, the page should not be directed to the folder index, instead, it should be refreshed with the current search queries results intact.

At the moment, when the user clicks on order, they are redirected to the index page, and in this process, lose whatever search results they had previously called.

I understand that I would need to add the users search string to a session, then recall this information after the page has been refreshed.

What I am struggling with though, is the process involved in this.

If anyone has any ideas on how i would go about this, I would really appreciate the advice!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:13:10+00:00Added an answer on May 24, 2026 at 2:13 pm

    My first thought is simply to stick either the $find or the $keywords_array into $_SESSION when you conduct the search. You’d need to update the code to check that $_SESSION variable as part of the process before issuing the “Enter a search term” error.

    Since you’re already using $_SESSION it is entirely possible that you’ve already headed down this road though. If that’s the case, what went wrong?

    Edit to include code — Basically, I would change the second code segment as follows:

    if (($searching == "yes") || (isset($_SESSION['find'])))

    …[your code]… until if ($find == "") which is replaced with:

    if (isset($_SESSION['find']) && ($find == "") 
       $find = $_SESSION['find'];
    
    if ($find == "")
    

    …[your code]… until $keywords_array = explode(' ', $find); which gets one line added before it:

    $_SESSION['find'] = $find;

    …[your code as before]…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a simple Prism 2.1 demo that uses the 'directory search' approach
I'm creating a simple API that creates typed classes based on JSON data that
I'm creating a simple tag system that allows me to link a tag to
I have pages within a site containing a control that uses a query string
I'm developing a simple articles website in brazilian portuguese language. The search feature is
I have an Excel spreadsheet of some product data, (just a simple table) that
I am creating a simple ASP page that has a Repeater control. This repeater
Need some help for creating a File and String search engine. The program needs
I am using Visual Studio 2008 Express and I tried creating a simple console
One simple method I've used in the past is basically just creating a second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.