Forums
This topic is locked
Decimals not uploading correctly
Posted 11 May 2006 17:31:52
1
has voted
11 May 2006 17:31:52 Carol Maurer posted:
Does anyone have any insight as to why my decimals are being rounded when they go into MYSQLI am in Dreamweaver8, and I pull the avg(rrate) for a particular book and create a variable called $avgrate. It echos out on my page as 3.667
However when I update that variable into MYSQL it shows up as 3.00
I've got my setting in MYSQL for that field at:
DECIMAL (4,2)
Does anyone know why I'm losing the 6?
It's driving me mad!
CLM
Using PHP, MYSQL, database driven website / Dreamweaver8
Replies
Replied 11 May 2006 19:11:16
11 May 2006 19:11:16 Roddy Dairion replied:
is your field type set as float?
Replied 11 May 2006 21:49:39
11 May 2006 21:49:39 Carol Maurer replied:
I finally figured it out. I was using '%u' instead of '%s' as my variable replacement in the update code.
Using PHP, MYSQL, database driven website / Dreamweaver8
Using PHP, MYSQL, database driven website / Dreamweaver8
Replied 12 May 2006 11:17:06
12 May 2006 11:17:06 Roddy Dairion replied:
Can you post the solution for others please.
Replied 13 May 2006 19:29:22
13 May 2006 19:29:22 Carol Maurer replied:
Here was my solution to my problem:
Reminder of problem: I could not get the decimal part of my number to get uploaded into my MYSQL database. Even though MYSQL was expecting a decimal, it wasn't receiving a decimal.
The problem was that I was using '%u' in my sprintf command.
%u has the argument treated as an interger.
However when I changed this to '%s' the upload carried through the decimal to my database. (%s is treated as a string, and evidently MYSQL could handle that and properly received it as a decimal (since that's what it was looking for).
Note that I also have a doubleval command on my decimal variable. To be honest, I don't know if it was necessary, but Dreamweaver uses when they perceive a decimal, so I added it as well.
Here is the successful code: I still left my bookid as a %u because it is an integer.
<pre id=code><font face=courier size=2 id=code> $updateSQL = sprintf("UPDATE books SET avg_rate= '%s' WHERE book_id= '%u'", doubleval($avgrate),$rbookid);</font id=code></pre id=code>
Using PHP, MYSQL, database driven website / Dreamweaver8
Reminder of problem: I could not get the decimal part of my number to get uploaded into my MYSQL database. Even though MYSQL was expecting a decimal, it wasn't receiving a decimal.
The problem was that I was using '%u' in my sprintf command.
%u has the argument treated as an interger.
However when I changed this to '%s' the upload carried through the decimal to my database. (%s is treated as a string, and evidently MYSQL could handle that and properly received it as a decimal (since that's what it was looking for).
Note that I also have a doubleval command on my decimal variable. To be honest, I don't know if it was necessary, but Dreamweaver uses when they perceive a decimal, so I added it as well.
Here is the successful code: I still left my bookid as a %u because it is an integer.
<pre id=code><font face=courier size=2 id=code> $updateSQL = sprintf("UPDATE books SET avg_rate= '%s' WHERE book_id= '%u'", doubleval($avgrate),$rbookid);</font id=code></pre id=code>
Using PHP, MYSQL, database driven website / Dreamweaver8