Jump to content

Edit database value with a calculation


Woncler

Recommended Posts

Hello,

 

I'm building a house point system for a virtual Hogwarts. I've managed to build up a form that saves its values to MySQL database, and I'm able to display them. The form has three important inputs: group, are the points added or subtracted, and the amount of points. Basically what I'd like to achieve is:

 

When user chooses Group 1, chooses to add points, chooses 5 as the point amount and submits the form, the form itself would save into independent table. But, depending on which group user chose and which action he chose, the point amount would be added (or subtracted) together with existing amount from this group 1, in another table.

 

I hope I explained this clearly :D So, in brief, is there a way to get a value from database, calculate it with the number that user typed, and update the value? Or is this kind of system too complicated for a total newbie like me?

 

All suggestions and help will be much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/273964-edit-database-value-with-a-calculation/
Share on other sites

You are making it more complicated than it needs to be. Simply store the group and point amount change (e.g. 5 or -5) no need to store a separate field to say weather the value is being added or subtracted.

 

Second, there is no need to use these individual records to update records in another table. You can simply get the total for a group/house by doing a query on the table that will sum up all the additions and subtractions.

 

SELECT groupName, SUM(points) AS totalPoints
FROM tableName
GROUP BY groupName

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.