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
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

Link to comment
Share on other sites

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.