Hi all:

I have this slight problem, where I have a list of months with amounts that need to be updated, but I always need to enter "0.0" in order for my field to be updated properly; but if I just enter "0" it doesnt take it and remains empty... ??? I'd like to simply enter 0 and have it update... am I missing a function? my table-field is setup as float(11,2). Here's the basic sql statement I'm using (each month refers to an amount to be updated). Is it beacuse I am NOT filtering the data as integers? or numbers??

$updateQuery = mysql_query("UPDATE BUDGETS SET Jan=$Jan, Feb=$Feb, Mar=$Mar, Apr=$Apr, May=$May, Jun=$Jun, Jul=$Jul, Aug=$Aug, Sep=$Sep, Oct=$Oct, Nov=$Nov, BUDGETS.Dec=$Dec, notes='$notes' WHERE recordid = $accountid AND recordcustid = '$member'");

Member Avatar for diafol

echo out your query:

echo $updateQuery;

and look to see if all the paramenters are entered.

When I manually enter 0 to a float (11,2) it is automatically converted to 0.00.

I'm no expert, but supplying '0' as a value could return a 'false' value??

EDIT
Er, actually, just tried entering a false value and it came up 0.00.
Also running a query with the field set to '' also gave 0.00.
However running a query without the field being declared in the SET clause, results in a NULL value.
Is your default value for these fields 0.00? If not, try this as it could 'trap' invalid entry values.

Hi.

Yes, be careful with a value of 0. Both PHP and MySQL interpret it as FALSE under certain circumstances.
And PHP, being a loosely typed language, is very aggressive in converting values when it needs to. (e.g. converting 0 into FALSE during boolean comparisons.)

I would just convert it as soon as possible into a string.
Like:

$number = $_POST['number'];
if($number == 0) {
  $number = "0.0";
}

Now PHP won't assume this is the number 0 (and thus FALSE), but rather a string with a value (and thus TRUE).

Thanks Ardav, Atli:

I think what's happening is kind of what Atli suggests... the ZERO keeps giving a FALSE result, instead of actuall 0 number. Otherwise, why would it take any other number BUT this "0". I use Dreamweaver, and I never have a problem with the scripts it automatically geneerates, so it I am siure it takes care of this integer-filtering issue thru validation, but I am trying to do things in my own and not be so dependent on Dreamweaver's generated code, since I find I always need to customize things so much after the fact. I did try something similar to what Atli wrote... but didnt seem to work, but maybe I set it up wrong.. gonna try his code instead. Also, as Ardav also suggests, I did put default field values to 0.00 in the database table, but didnt seem to help...

wish me luck! and thanks!

Hi, Atli:

You know, your code did seem to work... except now, even if the values in the form fields are left blank (no values at all in fields), it now automatically makes 0.00... I tried to change code a bit to recognize if it is "null" but didnt seem to work... I pretty much wanted database fields to also remain blank or null if posted value is "blank"...

This is your code: $Sep= $_POST['Sep']; if ($Sep == 0) { $Sep = "0.0";} This is what I tried to change to: $Sep= $_POST['Sep']; if ($Sep == 0) { $Sep = "0.0";} elseif(is_null($Sep)) { $Sep = 'NULL';} But it didnt seem to do it... but at least getting close!

I pretty much wanted database fields to also remain blank or null if posted value is "blank"...
[...]
This is what I tried to change to:
$Sep= $_POST; if ($Sep == 0) { $Sep = "0.0";} elseif(is_null($Sep)) { $Sep = 'NULL';}

By "blank" you mean empty; without a value, right?

If so, try

$Sep = trim($_POST['Sep']);
if($Sep === "0") {
    $Sep = '0.0';
}
else if(empty($Sep)) {
    $Sep = 'NULL';
}

A triple === checks for exact matches, no conversion allowed.
So doing $var = false; if($var === "0") the if will result in FALSE.

Thanks Atli.. sorry didnt answer earlier, had to step out for a while; let me try your code... hopefully it'll work. I guess I am still trying to grasp some of the special ways php sees null or empty spaces and the whole 0 thing... ok, let you know how it goes.

And yes, by "blank" i mean no value, completely empty

Thanks for your help again...

Hi, Atli.. nope, no luck... tried different versions of your code, but same thing. Maybe I got soemthing setup wrong in my db...

Thanks again... still trying...

If you echo $updateQuery before you execute it, how does it look like?
And how exactly does your table structure look like?

If you were to define your fields field float(11,2) NOT NULL Default 0.0 then passing it anything besides NULL and valid numbers would initialize it as 0.0

Also, if the accuracy of the values you are storing is important, then the float type is not the one to use. It can under some circumstances return your values slightly altered.
You should use decimal instead. field decimal(11,2) NOT NULL Default 0.0

Hey, Atli...

THANKS A LOT!!!! IT WORKED!!! as you suggested I went back to my table structure and changed it from float (11,2), to decimal(11,2), and it worked like a charm... I did go back to your other original code of:

$Sep = trim($_POST['Sep']); if($Sep === "0") { $Sep = '0.0';} else if(empty($Sep)) { $Sep = 'NULL';}

This made it possible for the "0" to be inserted as "0.00". What a difference this little change made. You know I had been using Float for most price-oriented fields (I think cause I had read somewhere to avoid decimals and use float instead for some reason). But seems to me using "Decimals" is the obvious choice, specially when it comes to prices. Having said that, why would someone therefore even want to use Float at all??? I am thinkiing now, the issue with decimals may have been something with doing some specific arithmetic functions with them?? can't recall...

At any rate... very much appreciate you keeping with me on this problem. Thanks a LOT...

I'm glad you got it working :)

[..]Having said that, why would someone therefore even want to use Float at all???

It's a good place to store data that won't necessarily be read, but rather used to create something. Or any value who won't suffer from a minor alteration.

Like say, the position of a player in a MMO, or the values used to draw a graph. A minor alteration to those kinds of values won't really be noticed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.