0

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 pending = pending-$qtd where id=$pid")  or die (pgs_log("erro linha 70 >".mysql_error())); 

So, am I missing something?

2
  • try to echo sql queries to the screen and see whether $qtd and $pid is still working, if it is not, check your pgs_log (not sure what does it do). Is the query giving and error or is the page just loading the but database unchanged? Commented May 24, 2011 at 17:50
  • you have a SQL-injection hole in your code, change where id=$pid") into where id='$id'"). i.e. enclose all those $vars in single quotes or your mysql_real_escape_string's will be for naught. Commented May 28, 2011 at 21:05

3 Answers 3

1

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()));
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.