Jump to content

Small problem with a query


McMaster

Recommended Posts

Hey,

 

I am trying to create a search query in MySQL where it will search the description and title from the database products. Now the only problem is that it doesn't actually search within that category I choose from the drop down but instead it searches all categories. Anyone know what I am doing wrong? This is my code:

 

$category = $_POST['category'];

$sql = "SELECT * FROM products WHERE category = '$category' AND description LIKE '%$cat_new%' OR title LIKE '%$cat_new%' OR keyword1 LIKE '%$cat_new%' OR keyword2 LIKE '%$cat_new%' OR keyword3 LIKE '%$cat_new%' OR keyword4 LIKE '%$cat_new%'";

[code=php:0]

 

I tried echoing the $category variable and the category I choose from the drop down is showing. Any help appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/206642-small-problem-with-a-query/
Share on other sites

I'm not a search expert, but I think this might be a precedence issue in the query string. Try forcing the precedence with parentheses.

 

$sql = "SELECT * FROM products WHERE category = '$category' AND (description LIKE '%$cat_new%' OR title LIKE '%$cat_new%' OR keyword1 LIKE '%$cat_new%' OR keyword2 LIKE '%$cat_new%' OR keyword3 LIKE '%$cat_new%' OR keyword4 LIKE '%$cat_new%')";

Define: that didn't work? What did it do, because that is exactly how you would make the query so that the specific category would match AND any one of the other terms would match. Are you sure you made the code change in the actual file you are using?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.