How to decrease a database field by 1 when user logs on????

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
lip9000
Forum Newbie
Posts: 11
Joined: Tue Jul 04, 2006 3:38 am

How to decrease a database field by 1 when user logs on????

Post by lip9000 »

I have a database with the fields username, password and sessions. Each user has a certain amount of sessions and I have setup my site to display how many sessions that user has left when he logs in.

How do I get it so that when he logs in, the sessions field under his id gets decreased by 1, so then his amount of sessions will be 1 less when he views it on the next page?

This would need like an update record function that occurs at the same time when the user clicks login, only if the login details are correct though.

What code would I need to do this? Any ideas? Thanks in advance.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

SQL:

Code: Select all

UPDATE users SET sessions = sessions - 1 WHERE id = $userid
Assuming your user table is called "users", your sessions column "sessions" and user id is "id".

Of course $userid would either be replaced with a bind variable or escaped appropriately beforehand
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

We have a Databases forum for a reason. Please be more careful where you post in the future.
Post Reply