tags:

views:

41

answers:

2

I'm sure this is something really simple, like a misplaced quote, but I'm completely stumped. I'm using an update query to enter shopping cart data into a mysql table, and having some major issues with card balance. The column in the mysql table is called BalanceCurrent and it's a decimal(18,2). In my php code (this is a custom Joomla component, so there are some Joomla objects and methods like JFactory and quote), here's what I'm doing:

$rightNow = date("YmdHis");
$db =& JFactory::getDBO();

$query = "update arrc_Voucher set BalanceCurrent = " . $db->quote(number_format($this->balanceRemaining,2)) . ", UpdateDT = ".$db->quote($rightNow). "  where VoucherNbr = ".$db->quote($this->voucherNbr);
error_log("update query: ".$query);
$db->setQuery($query);  
if (!$db->query()) error_log("error inside updateVoucher: ".$db->stderr());

The issue is this: $db->quote(number_format($this->balanceRemaining,2)). Without the quotes (created with $db->quote around the value) I get a mySQL 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 '950.00, UpdateDT = '20100813150330'  where VoucherNbr = '1142100120514884'' at line 1 SQL=update arrc_Voucher set BalanceCurrent = 4,950.00, UpdateDT = '20100813150330'  where VoucherNbr = '1142100120514884'

With the quotes, there's no error, and this is the SQL statement that results (printed to the error_log):

update query: update arrc_Voucher set BalanceCurrent = '4,950.00', UpdateDT = '20100813150508'  where VoucherNbr = '1142100120514884'

It looks fine, but the problem is, when I actually go into the database, the BalanceCurrent field is now set to 4.00, not 4950.00.

Any ideas? This is really killing me.

+2  A: 

I guess the thousands separator comma maybe a problem. What's the datatype of the field?

If you want to strip the thousands separator try: number_format($this->balanceRemaining,2, '.', '')

Guillermo
As I said, the field (column in the database) is decimal(18,2). The thousands separator is the default when using number_format in php. I'm not sure if there's any way to get rid of it. It's an optional param in the number_format function, but if you enter all four, you need to specify a separator. I'm not sure you can get rid of the separator by giving an empty string as the param. I'll try, though.
EmmyS
I'm sorry for not reading your post well:Try number_format($this->balanceRemaining,2, '.', '');
Guillermo
You need to pass a third and fourth param (third is decimal character, fourth is thousands): `number_format($number, 2, '.', '');`
ircmaxell
OK, I just tried number_format($this->balanceRemaining,2,'.','') and it does work. Thanks.
EmmyS
+1  A: 

Just do not use number_format() for inserting data into database. It's for displaying data. For pure rounding use round() function.

Mchl
Thanks, but we're not looking to round. The values will always be full dollars, no cents. We just need to make sure it ends up in the database with the .00 after it.
EmmyS
The field is DECIMAL so that doesn't matter. Wether you store `1`, `1.0`, `1.00` they will all result in `1.00` being stored. Best example is that when you tried to store `4,950.00`, it took just the front `4` and stored is as `4.00`
Mchl