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 8952253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:50:09+00:00 2026-06-15T13:50:09+00:00

Really hoping someone can help me with this. I’m building a PHP / MySQL

  • 0

Really hoping someone can help me with this. I’m building a PHP / MySQL search form that, hopefully, will allow users to search our wine database and filter the results according to a range of prices selected via a drop down menu.

The form works fine searching for, and returning, a nice list of accurate results. But it does NOT price filter the results.

After days of searching and experimenting, I’ve mashed together various code snippets to get this far but, overall, PHP is still very much a mystery to me.

It’s the correct coding and syntax I struggle with.

How might I code the PHP posted here to properly integrate the Price Range filter? I suspect my inclusion of “pricerange” in the sql query is way off base.

  • MySQL Server version: 5.1.65-cll
  • Price column type: decimal(10,2)

Any help would be hugely appreciated. Please check the code blocks below.

Thanks a ton!

HTML

 <form  method="post" action="winesearch.php?go" id="searchform"> 
 <input  type="text" size="35" name="user-entry"/>
 <select name="pricerange" size="1" id="pricerange">
    <option value="">Price Range&nbsp;</option>
    <option value="1">$&nbsp;10 - $20</option>
    <option value="2">$&nbsp;21 - $30</option>
    <option value="3">$&nbsp;31 - $50</option>
    <option value="4">$&nbsp;51 - $75</option>
    <option value="5">$&nbsp;76 - $100</option>
    <option value="6">$101 - $200</option>
    <option value="7">$201 - Plus</option>
</select> 
<input  type="submit" name="submit" value="Wine Search"/> 
</form>

PHP

<?php

  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[a-zA-Z0-9]+/", $_POST['user-entry'])){
  $cob=$_POST['user-entry'];
  $pricerange=$_POST['pricerange'];


  //connect to the database
  $db=mysql_connect  ("server", "user", "pass") or die (mysql_error());

  //-select the database to use
  $mydb=mysql_select_db("db_name");

  if($pricerange == 0) $pricerange = 1;

  switch ($pricerange) {
  case 1  :  $pricerange = " where Price BETWEEN 10.00 AND 20.00 ";  break; 
  case 2  :  $pricerange = " where Price BETWEEN 21.00 AND 30.00 ";  break;  
  case 3  :  $pricerange = " where Price BETWEEN 31.00 AND 50.00 ";  break;   
  case 4  :  $pricerange = " where Price BETWEEN 51.00 AND 75.00 ";  break;     
  case 5  :  $pricerange = " where Price BETWEEN 76.00 AND 100.00 ";  break;       
  case 6  :  $pricerange = " where Price BETWEEN 101.00 AND 200.00 ";  break;         
  case 7  :  $pricerange = " where Price > 200.00 ";  break;           
  }

  //-query the database table
  $sql="
    SELECT  ID, 
    CSPC, 
    Country,
    Producer,
    Wine,
    Year,
    Price 
    FROM winecellar WHERE 
    CSPC LIKE '%" . $cob .  "%' 
    OR 
    Country LIKE '%" . $cob ."%'
    OR 
    Producer LIKE '%" . $cob ."%'
    OR 
    Wine LIKE '%" . $cob ."%'
    OR 
    Year LIKE '%" . $cob ."%'
    OR 
    Price LIKE '%" . $pricerange ."%'
    ";

  //-run  the query against the mysql query function
  $result=mysql_query($sql);

  //-create  while loop and loop through result set
  while($row=mysql_fetch_array($result)){
    $CSPC=$row['CSPC'];
    $Country=$row['Country'];
    $Producer=$row['Producer'];
    $Wine=$row['Wine'];
    $Year=$row['Year']; 
    $Price=$row['Price'];
    $ID=$row['ID'];

    //-display the result of the array
echo  "<ul>\n";
echo  "<li>" . $CSPC . "</li>\n";
echo  "<li>" . $Country . "</li>\n";
echo  "<li>" . $Producer . "</li>\n";
echo  "<li>" . $Wine . "</li>\n";
echo  "<li>" . $Year . "</li>\n";
echo  "<li>" . "<a href=" . $Price .  ">" . "$" . $Price . "</a></li>\n";

echo  "</ul>";
  }
  }
  else{
  echo  "<p>Please enter a search query</p>";
  }
  }
  }
?>
  • 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-06-15T13:50:10+00:00Added an answer on June 15, 2026 at 1:50 pm
    <?php
    
      if(isset($_POST['submit'])){
      if(isset($_GET['go'])){
       // improved the filter to support space and -
       // Also closed critical security breache (SQL-injection)
      if(preg_match("/^[a-zA-Z0-9 -]+$/", $_POST['user-entry'])){
      $cob=$_POST['user-entry'];
      $pricerange=$_POST['pricerange'];
    
    
      //connect to the database
      $db=mysql_connect  ("server", "user", "pass") or die (mysql_error());
    
      //-select the database to use
      $mydb=mysql_select_db("db_name");
    
      switch ($pricerange) {
      case 2  :  $pricerange = " AND Price BETWEEN 21.00 AND 30.00 ";  break;  
      case 3  :  $pricerange = " AND Price BETWEEN 31.00 AND 50.00 ";  break;   
      case 4  :  $pricerange = " AND Price BETWEEN 51.00 AND 75.00 ";  break;     
      case 5  :  $pricerange = " AND Price BETWEEN 76.00 AND 100.00 ";  break;       
      case 6  :  $pricerange = " AND Price BETWEEN 101.00 AND 200.00 ";  break;         
      case 7  :  $pricerange = " AND Price > 200.00 ";  break;
      default :  $pricerange = " AND Price BETWEEN 10.00 AND 20.00 "; // covers all other cases
      }
    
      //-query the database table
      $sql="
        SELECT  ID, 
        CSPC, 
        Country,
        Producer,
        Wine,
        Year,
        Price 
        FROM winecellar WHERE 
        (CSPC LIKE '%" . $cob .  "%' 
        OR 
        Country LIKE '%" . $cob ."%'
        OR 
        Producer LIKE '%" . $cob ."%'
        OR 
        Wine LIKE '%" . $cob ."%'
        OR 
        Year LIKE '%" . $cob ."%')
        " . $pricerange;
    
      //-run  the query against the mysql query function
      $result=mysql_query($sql);
    
      //-create  while loop and loop through result set
      while($row=mysql_fetch_array($result)){
        $CSPC=$row['CSPC'];
        $Country=$row['Country'];
        $Producer=$row['Producer'];
        $Wine=$row['Wine'];
        $Year=$row['Year']; 
        $Price=$row['Price'];
        $ID=$row['ID'];
    
        //-display the result of the array
    echo  "<ul>\n";
    echo  "<li>" . $CSPC . "</li>\n";
    echo  "<li>" . $Country . "</li>\n";
    echo  "<li>" . $Producer . "</li>\n";
    echo  "<li>" . $Wine . "</li>\n";
    echo  "<li>" . $Year . "</li>\n";
    echo  "<li>" . "<a href=" . $Price .  ">" . "$" . $Price . "</a></li>\n";
    
    echo  "</ul>";
      }
      }
      else{
      echo  "<p>Please enter a search query</p>";
      }
      }
      }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really hoping that someone can help. I have tomorrow to get this right
I'm really struggling with a MySQL query that I'm really hoping someone can help
I'm really hoping someone can help me out with this one. I've been stuck
I'm hoping someone can help with this, I'm having a really difficult time getting
I really can't get this working so i'm hoping someone here can help :)
Really hoping someone can help with this. I'm sure there are better ways to
I'm running across a very frustrating problem that I'm really hoping someone can help
I am really hoping someone can help me out, because I'm really stuck on
I've never been much for math and I'm hoping that someone can help me
I'm hoping someone can help me decide the best way to model this design

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.