Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

having some syntax issue with a PHP based MySQL query, how to add another "where" clause?

 } else {
    $sql = "update products 
                set products_quantity = '" . $qty . "', 
                    products_last_modified = '" .date("Y-m-d H:i:s")."' 
            where " . PRODUCT_ID . "= '" . $model_no . "'";

    $result = mysql_query($sql);      // this executes the sql
  }
 } 
}                                   // back up to process next item

// and finally, close the db and we are done.
tep_db_close();

?>

credit for code to original author for a open source plug in posted online, currently the query is updating a product record’s stock level WHEN the product ID passed to it matches and also sets the last modified date.

I am also trying to add another where clause whereby the sql query needs to also only perform the record update if "products_status = 1" this is another field in the MySQL database and I only want to update records where this flag is on 1 . I tried adding this line in but I am clearly messing up the syntax.

Please don’t laugh but I tried something like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    $sql = "update products 
                set products_quantity = '" . $qty . "', 
                    products_last_modified = '" .date("Y-m-d H:i:s")."' 
            where " . PRODUCT_ID . "= '" . $model_no . "' 
            AND " . products_status . "= '" . "'1" . "'";

and it didn’t work

>Solution :

I think you are 90% there. The general form of a WHERE clause is:

WHERE
   field_1 = 'some specific value' AND
   field_2 = 1233  -- also a specific value

The original code you have for your WHERE clause is:

           where " . PRODUCT_ID . "= '" . $model_no . "'";

It seems to me here that PRODUCT_ID is a variable that is containing the name of a field and $model_no is the value. Although, it is possible that I have that backwards.

The code you added was:

AND " . products_status . "= '" . "'1" . "'"

I’m not sure if products_status is a variable containing the name of a field, but I’m guessing it is. Now, in the rest of the line there is an issue. You have " '" . "'1" . "'" This would evaluate to ''1', which is one too many single quotes. If product_status is a varchar or similar, you just want "='1'" if it is a numeric type you want "=1".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading