PNewCode Posted February 6, 2023 Share Posted February 6, 2023 (edited) 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 February 6, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/ Share on other sites More sharing options...
ginerjm Posted February 6, 2023 Share Posted February 6, 2023 Why is $user_token in parens? And why is your if statement doing the same thing regardless of the existence of submit3? Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605424 Share on other sites More sharing options...
PNewCode Posted February 6, 2023 Author Share Posted February 6, 2023 @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 Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605425 Share on other sites More sharing options...
Barand Posted February 6, 2023 Share Posted February 6, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605426 Share on other sites More sharing options...
PNewCode Posted February 6, 2023 Author Share Posted February 6, 2023 @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? Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605427 Share on other sites More sharing options...
PNewCode Posted February 6, 2023 Author Share Posted February 6, 2023 (edited) The 0 looks like a P in the screen shot because my cursor was there but it's a 0 (zero) Edited February 6, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605428 Share on other sites More sharing options...
Solution requinix Posted February 6, 2023 Solution Share Posted February 6, 2023 DECIMAL(65,0) means it can store a number with 65 digits and 0 after the decimal point. Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605429 Share on other sites More sharing options...
PNewCode Posted February 6, 2023 Author Share Posted February 6, 2023 @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! Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605430 Share on other sites More sharing options...
ginerjm Posted February 6, 2023 Share Posted February 6, 2023 Happy that you db problem was cleared up. But getting back to my question, why are you wrapping var names in parens? Completely unnecessary. Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605431 Share on other sites More sharing options...
PNewCode Posted February 6, 2023 Author Share Posted February 6, 2023 @ginerjmif you saw the rest of the pages scripting you would see why. Sorry but I didn't find it necessary to post the whole thing since it wasn't relevant to my issue. All is working perfect now Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605434 Share on other sites More sharing options...
ginerjm Posted February 6, 2023 Share Posted February 6, 2023 Regardless of how the rest of your code is written, the simple use of a variable in a calculation does not require parentheses around it. Just trying to teach here. Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605435 Share on other sites More sharing options...
PNewCode Posted February 6, 2023 Author Share Posted February 6, 2023 Okay then, thanks Quote Link to comment https://forums.phpfreaks.com/topic/315885-subracting-a-number-from-decimal-value-in-mysql/#findComment-1605438 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.