Portal Home > Knowledgebase > Articles Database > PHP arithmetic issue
PHP arithmetic issue
Posted by FinalFrontier, 11-23-2011, 04:26 AM |
I am trying to perform an arithmetic function:
My code that I tried to use:
$query = "UPDATE products SET sizes = '".$row['sizes'] - $content['sizes']."' WHERE id = 1;
This didn't work and I suspect it is because I am braking out wrong or something.
When I use this (as a test) it works fine:
$query = "UPDATE products SET sizes = 1 WHERE id = 1;
When I use this, I get the correct value:
echo $row['sizes'] - $content['sizes'];
Very strange! Can anyone see whats going on? The error log didn't report anything, but then I didn't expect it to.
|
Posted by Squidix - SamBarrow, 11-23-2011, 04:28 AM |
You need to put parentheses around any mathematical operations. Otherwise it concatenates the string before doing the calculation. Then you wind up subtracting two strings.
|
Posted by FinalFrontier, 11-23-2011, 04:30 AM |
Thanks dude! I'll look into this and tell you how it went!
EDIT:... and your right.. before I echoed the whole query (with the PHP) and I got a minus 1!
|
Posted by Squidix - SamBarrow, 11-23-2011, 04:30 AM |
query = "UPDATE products SET sizes = '".($row['sizes'] - $content['sizes'])."' WHERE id = 1;
Should take care of it.
|
Posted by FinalFrontier, 11-23-2011, 04:35 AM |
You were correct, Sam.
I refered to some arithmatic code I wrote up a couple of months back and it does have the brackets (the Greek word) around the sum! Thanks dude, you've been a great help, really!
|
Posted by FinalFrontier, 11-23-2011, 04:45 AM |
You should put a PayPal donation link in your signature.
|
Posted by Squidix - SamBarrow, 11-23-2011, 04:48 AM |
Haha. I'm usually not this helpful as there aren't too many PHP questions being asked here.
|
Add to Favourites Print this Article
Also Read