Hello everyone,

I am setting up a section of coding in which I need to add and subtract certain values to a mysql field. However ...the code only seems to add negative numbers (-13.0) as a postive number to the database and its really confusing me now! :(

What I have is a field in my database that contains a number that changes regulary.

For example say the number is -0.4.

I am trying to set this code up so that it multiplies the above number by 10 for example which would make it -4.0 and then inserts this new number into another database field.

However ....because the new number is a negative number I need the code to minus this value from the total in the database field!

Instead ...all it does is adds it to the database total as a postive number. Ive searched google and cannot seem to work this little problem out! :(

Here is my current code. because it didnt work by simply trying to add the new value to the current total in the database ...I have tried to first determine if the amount is above or below 0.0. If its below then the code should minus the new value from the amount and if its above 0.0 then it should add the new value to the amount but it doesnt work. Its still just adding the new value regardless of whether its negative or postive.

Any help would really be much appreciated!

Thank you

/////////start to calculate each  total value!
/////////////////////////////////////////////////
	$value = fetch ("SELECT * FROM material WHERE name='value'");
	$units = $value[current] * $find_place[monthly_total];
	
	
	///////does it go up or down
	IF ($value[current] >=0.1)
	{
	$updown ==1;
	}
	else
	{

	$updown ==0;
	}

	IF ($updown ==1)
	{
	
	mysql_query("UPDATE places SET totaling=totaling+ $units WHERE  area='1'");
	}
		IF ($updown ==0)
	{

		mysql_query("UPDATE places SET totaling=totaling- $units WHERE  area='1'");
	}
	

	die(header(error("result.php?game=$game","Your update was successfull!")));

I don't undertand the meaning of your question... Why not to use an incrementing/decrementing operators (e.g. $updown--) or assignment operator. The values inside the braces doesn't contain an assignment operator, so doesn't change their value when the condition is true.

Hiya,

Thanks for the advice. Ill go and look that up as im not familiar with that.

Can I ask why the code doesnt do the sum as normal maths!

For example the sum 20 + -10 = 10.

Why does it not take note of the minus sign in a normal sum?

Thanks

There is a little mistake, on your code you're using a comparing operator (double equal signs) instead of assignation (one equal sign). Try with this example:

<?php 

$a = 20;
$a = $a + (-10);

echo $a;

?>

It's the same:

<?php 

$a = 20;
$a += (-10);

echo $a;

?>

Hii...
By default, php assumes that the type of the variable is string..
So this also sometimes create a confusion for compiler as well..

So while you do mathematical calculations, it happens that it does not calculate value, rather it apends it... just because it is a string..

So whenever you calculate something.. use intval() or floatval() function to convert the type of variable from string to int/float..

Also in your code you have used comparison operator(==) to assign a value, rather than assignment operator (=) which is used....
So take care of this when u code...

Hope this helps..!!

Hello,

Thanks for your help! I seem to have sorted it now which is great! :) Thanks for the explanation. My knowledge of PHP is somewhat scatty. Im still learning so this has been a good experience.

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.