Dimitar Ivanov
Dimitar Ivanov

Reputation: 31

MySQL sql ignoring WHERE condition

I have the following problem - I am coding an e-commerce website, that has promotions for a certain period of time. When time elapses promotion changes its corresponding database active value to 0. When I check for promotions the first condition is that active=1, but at some cases MySQL is ignoring it.

Here is an example of my most recent problem:

    $productPromotion = $db->getResults('*', TABLE_PROMO, "active = '1' 
AND (discount_subject = 'all_orders' 
OR discount_subject_product = ".$values['product']['id'].") 
OR (discount_subject = 'category' 
AND discount_subject_category = ".$categoryId[0] . ") ORDER BY id ASC");

$db->getResult is a custom function that takes 3 parameters - What, Table and Where.

The problem is that it is returning promotions that are already expired and have active=0. Where is the problem with my sql?

Upvotes: 0

Views: 67

Answers (1)

Jens
Jens

Reputation: 69440

You have to add brackets arround or

 $productPromotion = $db->getResults('*', TABLE_PROMO, "active = '1' 
      AND 
     ((discount_subject = 'all_orders' OR discount_subject_product = ".$values['product']['id'].")
        OR (discount_subject = 'category' AND discount_subject_category     = ".$categoryId[0] . ")) ORDER BY id ASC");

Also learn about prepared Statements to prevent SQL-injection

Upvotes: 1

Related Questions