Jump to content

Subracting a number from decimal value in mysql


Go to solution Solved by requinix,

Recommended Posts

Hello wise and wonderful people!

Today I am tackling a task to make a function to subtract a value by decimal from mysql database.
I am including the part that is the functioning part of the script to show.
Right now, it works perfect for whole numbers (1, 3, 9, etc). In this bit, you can see it's at 1

The problem is, when I change this to .5 then it doesn't subtract anything at all.
Could this be because in my database I have the column set to int(225) - null?

PS: I only included this part because everything else is just connections and images and stuff like that. So far, what you see below works perfect for whole numbers.

Edit: The goal is to subtract 1/2 token (number) so if the user has 3 tokens, then this will bring it to 2.5 tokens
 

 if(isset($_POST['submit3'])){
$user_updated_token = ($user_token) - 1;
     
 }else{
    
$user_updated_token = ($user_token) - 1;
}

  $sql_l = "UPDATE users SET token='$user_updated_token' WHERE id=$user_id";

 

Edited by PNewCode

@ginerjmAll of that is because other parts of the page require it for other functions to work. But that's not what I'm posting about. I'm asking how to make it so this can deduct a half of a number instead of a whole number. Thats why I only included this bit because that is the function I'm working with

1 minute ago, PNewCode said:

Could this be because in my database I have the column set to int(225)

Of course it could - int type columns can only store whole numbers (integers).

Change the column type to FLOAT or DECIMAL.

@BarandThank you for that. However that didn't do the trick. I changed it to DECIMAL but it's still subtracting 1 instead of .5 (I changed the script that I posted to be .5 instead of 1). Am I missing something in the script itself?

@requinix GOT IT! Thank you. Between you and @Barandyou solved it. However this brings a new problem. Thank you so much for the info. (I changed the 0 to a 1). I'm marking Requinix as the solution but credit goes to you both :)

I also learned something here AGAIN. I love this site!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.