Count down postings remaining for user

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
admin2gd1
Forum Newbie
Posts: 15
Joined: Fri Aug 15, 2008 2:21 pm

Count down postings remaining for user

Post by admin2gd1 »

I am trying to update the amount of postings remaining down everytime a user posts so if the user had a limit of 200 posts then when the user posts it counts down by 1 and updates the table field for that user to 199.

Now the error that I am getting is that it does not count down correctly and puts 20 instead and then the next time the user posts after that it will put 0.

Code: Select all

$remain = mysql_query("SELECT postings_remaining FROM ".TBL_USERS." WHERE username = '$session->username' limit 1");
        $postings = $remain; //Count the number of postings remaining the user has
        $postingsremain = $postings - 1; //Remove 1 to the users posting remainings
        $sqlcountdown = mysql_query("UPDATE ".TBL_USERS." SET postings_remaining = '$postingsremain' WHERE username = '$session->username'");
 
If someone can help me with this function I would very much appreciate it.
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: Count down postings remaining for user

Post by starram »

Hey when a user posts you must be storing his id somewhere in database. just count all those records using his id and deduct them from 200 or whatever is the limit. You will get the remaining posts.
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: Count down postings remaining for user

Post by dajawu »

You need to fetch the data from the query using mysql_fetch_row first. Try this code, be sure to add error checking in case the user doesn't exist, etc...

Code: Select all

 
$remain = mysql_query("SELECT postings_remaining FROM ".TBL_USERS." WHERE username = '$session->username' limit 1");
$remain = mysql_fetch_row($remain);
$postings = $remain[0]; //Count the number of postings remaining the user has
$postingsremain = $postings - 1; //Remove 1 to the users posting remainings
$sqlcountdown = mysql_query("UPDATE ".TBL_USERS." SET postings_remaining = '$postingsremain' WHERE username = '$session->username'");
 
admin2gd1
Forum Newbie
Posts: 15
Joined: Fri Aug 15, 2008 2:21 pm

Re: Count down postings remaining for user

Post by admin2gd1 »

That worked great, thank you.
Post Reply