1

I have an MySQL error and I cannot figure it out... It was working and now it is not working anymore (I haven't done anything to the database).

Here is the query:

$querySearch = "SELECT * FROM cars WHERE (price >= {$startPrice} AND price <= $endPrice) AND condition = '{$condition}'";

Here is the form:

<h3>Condition:</h3>
<span class="searchRange">
<select name="condition" class="condition">
    <option value="any">Any Condition</option>
    <option value="Brand New">Brand New</option>
    <option value="Near New">Near New</option>
    <option value="Good">Good</option>
    <option value="Ok">Ok</option>
    <option value="Poor">Poor</option>
</select>
</span>

And here is how PHP receives the data:

$condition = mysql_prep($_POST['condition']);

mysql_prep() is a user made function that just takes care of the magic quote stuff.

Anyone see where an error could occur?

Error: Database query failed: 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 'condition = 'Near New'' at line 1

joerick
  • 16,078
  • 4
  • 53
  • 57
Cow
  • 764
  • 5
  • 10
  • 25
  • It would help if you told us *what* the error was... – sachleen Dec 06 '12 at 18:32
  • you should really be using a prepared statement for this instead of doing your own escaping. – Daniel A. White Dec 06 '12 at 18:32
  • Could you tell us what error is given from PHP and MySQL? Have you tried writing the query directly into Workbench/PHPMyAdmin? – Jeff Noel Dec 06 '12 at 18:32
  • Database query failed: 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 'condition = 'Near New'' at line 1 – Cow Dec 06 '12 at 18:33
  • possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – Ian Ringrose May 06 '14 at 09:56
  • Ian... I posted this over a year ago. That question you linked was asked last month. – Cow Jun 06 '14 at 00:54

1 Answers1

6

Condition is a reserved keyword

Enclose it in backticks.

AND `condition` =
sachleen
  • 30,730
  • 8
  • 78
  • 73
  • Wow... Thank you a bunch, saved me a lot of heartache! Will accept when time is up. Thank you again. – Cow Dec 06 '12 at 18:34