开发者

Multiple mysql_query not working

I have a working mysql_query:

mysql_query("update products set buyers = buyers+$qtd where id=$pid")  or die (pgs_log("erro linha 70 >".mysql_error()));   

But then I insert the following query right after it, and it only execute the first one:

mysql_query("update products set pendin开发者_StackOverflow中文版g = pending-$qtd where id=$pid")  or die (pgs_log("erro linha 70 >".mysql_error())); 

So, am I missing something?


Couple of things. First off you don't need two separate queries for this. MySQL may be confused into thinking your value is a column name because of the dash:

mysql_query("
UPDATE `products` 
SET `buyers` = `buyers` + $qtd, 
`pending` = `pending` - $qtd 
WHERE `id` = $pid")  or die (pgs_log("erro linha 70 >".mysql_error()));


f you wanted to double check that your update actually updated the data then you should investigate mysql_affected_rows. You'll need to check that your old value was different to your new value though, otherwise you'll have zero affected rows, making it a useless check.

You don't use proper quoting around the table and column references. These should be surrounded with back ticks, and could be combined, like the following:

UPDATE
    `products` 
SET
    `pending` = `pending` - $qtd,
    `buyers` = `buyers` + $qtd
WHERE
    `id` = $pid;


mysql_query("update `products` 
             set `pending` = `pending` - $qtd, 
                 `buyers` = `buyers` + $qtd 
             where `id` = $pid")  
             or die (pgs_log("erro linha 70 >".mysql_error())); 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜