PDA

View Full Version : Count down postings remaining for user



admin2gd1
09-04-2008, 12:29 PM
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.


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

BenInBlack
09-04-2008, 01:12 PM
//You don't really need the "limit 1"
$remain = mysql_query("SELECT postings_remaining FROM ".TBL_USERS." WHERE username = '$session->username' limit 1");
//I'm questioning the where here. see note 2

//not sure why you need this extra var setting, you didnt give all code
$postings = $remain; //Count the number of postings remaining the user has

//this line is fine
$postingsremain = $postings - 1; //Remove 1 to the users posting remainings

//not sure why you have single quotes around an integer
$sqlcountdown = mysql_query("UPDATE ".TBL_USERS." SET postings_remaining = '$postingsremain' WHERE username = '$session->username'");




Note 1: on the last line, since you are alway decrementing by 1 you can do that in the update statement.
example: update table set column = column - 1 where clause

Note 2: Session var is $_SESSION, I alway reference sessions like this
$_SESSION['username']

need more of code to figure out but

admin2gd1
09-04-2008, 02:51 PM
I have the function working now so thanks any way for your help.