1

I am developing a website where by people can buy and sell new or used cars. I want to set up a simple filter using php and sql statements and also of course html select boxes (drop down options). I have gotten all the other options to work with help and trust me it has been greatly appreciated. However i have been trying to get the "price filter" to work and been getting no where. Below is what my select box in html look like.

<select name="price" size="0">
<option value="100000">Under $100,000</option>
<option value="<?php echo " BETWEEN '100000' AND '250000' "?>">Between $100,000 and $250,000</option>
<option value=">250000<500000">Between $250,000 and $500,000</option>
<option value=">500000<750000">Between $500, 000 and $750,000</option>
<option value=">7500000">Over $750,000</option>
</select>

This is my $_POST statement for the price variable:

$price = mysql_real_escape_string($_POST['price']);

This is my sql statement:

$sql = sprintf("SELECT * FROM  `chjadb_vehicles` WHERE  `v_make` LIKE  '$make'AND `v_year` =  '$year'AND  `v_trans` LIKE  '$trans'AND  `condition` LIKE  '$cond' AND `b_style` LIKE '$b_style' AND `v_price` '$price'");

After which i have a basic loop that should print the results. Now from what i understand (correct or not), when the user selects a specific option from the drop down menu the value from "option value" is passed to the $_POST statement. After which it should replace $price with the actual text BETWEEN '100000' AND '250000', completing the query statement. So the final statement printed from the sql statement should be:

"SELECT * FROM  `chjadb_vehicles` WHERE  `v_make` LIKE  '$make'AND  `v_year` =  '$year'AND  `v_trans` LIKE  '$trans'AND  `condition` LIKE  '$cond' AND  `b_style` LIKE '$b_style' AND `v_price` BETWEEN '100000' AND '250000'"`

However i get this error:

BETWEEN \'100000\' AND \'250000\' You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' BETWEEN \'100000\' AND \'250000\' '' at line 8

Any ideas as to where i went wrong? Thanks

Regards, D. Ruddock

3
  • Why won't you simply use WHERE v_price >= 100000 AND v_price <= 250000? Commented Jul 11, 2012 at 20:47
  • what is a "select boes"? Commented Jul 11, 2012 at 20:47
  • That should have been "Select Boxes"... Sorry about the typo correction made... When i use the statement you selected i get the following php error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''>= 100000 AND v_price <= 250000'' at line 8 Commented Jul 11, 2012 at 21:36

3 Answers 3

1

After the line:

$sql = sprintf("SELECT * FROM `chjadb_vehicles` WHERE `v_make` L....

put:

echo "<br><br>".$sql."<br><br>";

temporarily, and look at the final SQL query in the browser when using the page. Find where the error is and then remove the "echo" statement.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm pretty new myself, but I don't think you need the quotes around the numbers in your BETWEEN statement. I think you can take them out. If I'm wrong I'm sure I'll be corrected, but that's my thought.

1 Comment

The quotations shouldn't be there but i automatically generated the PHP code with PHPMYADMIN and they included the quotations so i left them in
0

Is the ` at the very end a typo? If it is not, that might be the problem.

1 Comment

yeah not sure how that got there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.