在发布主题/回复时增加用户帖子计数

发布于 2024-10-14 19:00:22 字数 531 浏览 5 评论 0原文

我对 SQL 很陌生,在网上寻找有关增加值的帮助,但找不到适合我的东西。我想做的是在发布回复/主题时增加 user_posts 的值。 user_posts位于用户中(论坛>用户>用户帖子)。我不知道如何获取特定用户,然后添加他/她的帖子的价值。我正在研究一个非常小且基本的示例,这就是我发布帖子的方式。我想我可以将它添加到将主题发送到数据库的 SQL 中,但是,就像我说的,不知道如何做。我真的没有任何代码可以开始,因为我真的不知道从哪里开始。我相信它使用 UPDATE,但这就是我所能猜测的。如果您在阅读完所有内容后仍不太确定我想要什么,请询问,我会尽力澄清。

这是部分工作后代码和似乎不起作用的建议代码的样子。

VALUES ('" . $_POST['reply-content'] . "',
NOW(),
" . mysql_real_escape_string($_GET['id']) . ",
" . $_SESSION['user_id'] . ");
UPDATE users SET user_posts=1 WHERE user_id=1";

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 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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-10-21 19:00:22

你可以写

UPDATE Users
SET PostCount = PostCount + 1
WHERE UserId = @id

You can write

UPDATE Users
SET PostCount = PostCount + 1
WHERE UserId = @id
清旖 2024-10-21 19:00:22

类似于: select postcount from users where user_id =; 其次是 update users set postcount=;其中 user_id =;。一些详细信息: http://www.w3schools.com/sql/sql_update.asp

我除非您在处理负载时遇到问题,否则不会担心优化对数据库的查询。 :)

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文