Jump to content

Update + Add


ChidoriSoul
 Share

Recommended Posts

Hello. I am currently trying to run a site where you can randomly find some "credits" on the ground.I am currently trying to make a system where a user finds them, then it adds a set amount (5) to a field in my database. So if a user has 100 credits, then goes on the page, he/she would now have 105. Anyone to help me with this?

Link to comment
Share on other sites

"UPDATE credit SET amount = amount+5 WHERE userid = $_SESSION["userid"]";Change field names to match the ones in the table, and however you identify the user. If you are saving this in a table with the field for credit already in existence even if empty, that's fine. If you are saving the credit in a new table, then you will need to check if they have a record already. If not, you need to create one, if so, you need to update. I just discovered a new condition - ON DUPLICATE KEY UPDATE, which allows you to do both at once - if there is already a row with the unique index set, the table is updated, otherwise a new row is made.Definately worth checking out for this unless the field is already in another table which, by design, has a record for the user.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...