Increment user postcount upon posting topic/reply
I'm really new to SQL and looked around the web for help on incrementing values but couldn't find something that works for me. What I'm trying to do is increment the value of user_posts when a reply/topic is posted. The user_posts is in开发者_C百科 users (forums>users>user_posts). I don't know how to get the specific user and then add to the value of his/her posts. I'm working off of a very small and basic example, which is how I have posts working. I was thinking I could add it to the SQL where the topic is sent to the DB but, like I said, can't figure out how. I don't really have any code to provide to start with because I really don't know where to start. I believe it uses UPDATE, but that's all I can guess. If you aren't quite sure what it is I want even after reading all of this, ask and I'll try to clarify.
Here's what it looks like with part of the working post code and a suggested code that doesn't seem to work.
VALUES ('" . $_POST['reply-content'] . "',
NOW(),
" . mysql_real_escape_string($_GET['id']) . ",
" . $_SESSION['user_id'] . ");
UPDATE users SET user_posts=1 WHERE user_id=1";
You can write
UPDATE Users
SET PostCount = PostCount + 1
WHERE UserId = @id
Something like: select postcount from users where user_id = <userid>;
followed by update users set postcount=<new_postcount> where user_id = <userid>;
. Some details: http://www.w3schools.com/sql/sql_update.asp
I wouldn't worry about optimizing your queries to your database until you experience problems handling your load. :)
精彩评论