Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.