开发者

Need help with multiple WHERE clause

开发者 https://www.devze.com 2023-02-07 23:26 出处:网络
I have an UPDATE query I\'开发者_运维技巧d like to perform. But there need to be 2 conditions met before we can update a quantity in the database.

I have an UPDATE query I'开发者_运维技巧d like to perform.

But there need to be 2 conditions met before we can update a quantity in the database.

First, the sessions_id() must match one of the sessionid's in the sessionid column, and second, the Description of the product must match the description of the product corresponding to the sessionid.

Here's what I have:

mysql_query(UPDATE cart 
               SET quantity = $q, 
             WHERE sessionid = "'.session_id().'" 
               AND description = $d') or die(mysql_error());

Now, it is giving me the following error:

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 'WHERE sessionid="bqlbh5rdogbhmq70skhtkbvmb0" AND description=$d' at line 1

However, I have copied this update query straight from W3Schools, so it must be correct, right? Any help at all would be appreciated.


The error occurs because of the ',' sign before the WHERE statement. The correct query would be:

mysql_query( "UPDATE cart SET quantity = " . $q . " WHERE sessionid = "' . session_id( ) . '" AND description = " . $d . "" ) or die( mysql_error( ) );


remove the , (comma) after $q


the code you have pasted is badly formatted, and i doubt the code used in your app.

$sql = sprintf(
    "UPDATE `cart` SET `quantity` = '%d' WHERE `sessionid` = '%s' AND `description` = '%s'", 
    $q, 
    session_id(), 
    $d
);
mysql_query($sql);


It should look that way:

mysql_query("UPDATE cart SET quantity = $q WHERE sessionid='" .session_id(). "' AND description = '$d'") or die(mysql_error());


Also you have problem with the single quotes here .'" AND description=$d') and $d is not parsed. This will result wrong SQL query. Change the single quotes to double quotes or just use string append . to append the value of $d, not the literal $d

example :

$a = 5;
$b = 'a is $a'; // a is $a;
$c = "a is $a"; // a is 5
0

精彩评论

暂无评论...
验证码 换一张
取 消