如何通过 AJAX 将增加的选票存储在数据库中?

发布于 2024-11-10 03:44:37 字数 1612 浏览 2 评论 0原文

我正处于我的第一个 AJAX 项目的最后阶段。我有一个有评论的社交网络。我只是添加一个拇指图标,按下该图标时,会通过 JQUERY 将评论的 id 发送到后台 php 页面,该页面应该存储评论的 id 以及被按下的记录(增量)。发生这种情况后,我需要它将记录发送回拇指图标所在的页面,并告诉该页面拇指已被击中并增加指定区域中的计数器。

thumb_increment 表中,我有:

id 自动递增并且 comment_id 这是正在投票的评论的 ID。

我想知道是否应该添加另一列来保存点赞数,或者只在 comment_id 列中进行跟踪。我对那里的逻辑有点困惑。

到目前为止,我可以单击拇指并让它从我的其他页面发送 id,并通过 $_POST 获取 id。这是代码:

<?php

// 1. CHECK AND SEE IF THE "$comment_id" IS VALID. I AM GOING TO RETREIVE THE VALUE OF THE $_POST BEING SENT FROM THE PHP PAGE THAT IS SENDING THE REQUEST

/* QUERY TO CHECK $_POST DATA WITH: */

/* this is grabbing id that jquery sent over via post */
if(isset($_POST['comment_id'])) {

/* making a variable out of the grabbed id */   
$retreived_comment_id = ($_POST['comment_id']); 

/* this query is bring to frutation the exact id of the comment */
$query = "SELECT * FROM `CysticAirwaves` WHERE `id` = '".$retreived_comment_id."' && `status` = 'active'"; 
$request = mysql_query($query,$connection);
if($result = mysql_fetch_array($request)) {

/* insert the comment into the increment table */

$query = "INSERT INTO `thumb_increment` (
                                `comment_id`
                            ) VALUES (
                            '" . $retreived_comment_id . "'
                                )";
mysql_query($query,$connection);

/* increment the vote in the db */

    }

}


?>

所以总结一下我剩下要做的事情:

我需要增加评论并将其存储在我的数据库中,将其放入一个变量中,该变量发送回带有拇指图标的页面,然后将其存储在数据库中。可变钉在计数器上的增量

提前致谢。

I am on the final leg of my very first AJAX project. I have a social network that has comments. I am simply adding a thumb icon, that when pressed, sends the id of the comment via JQUERY to a background php page that is SUPPOSED to store the id of the comment as well as a record of it being pressed ( increment ). After that happens, I need it to send a record of that back to the page the thumb icon is on, and tell that page that the thumb has been hit and increment the counter in the designated area.

In the thumb_incrementtable I have:

id that is auto incremented and
comment_id that is the id of the comment that is being upvoted.

I am wondering if I should add another column to hold the amount of upvotes or just keep track in the comment_id column. I am a little confused on the logic there.

So far I am able to click the thumb and have it send over the id from my other page and grab the id via $_POST. Here is that code:

<?php

// 1. CHECK AND SEE IF THE "$comment_id" IS VALID. I AM GOING TO RETREIVE THE VALUE OF THE $_POST BEING SENT FROM THE PHP PAGE THAT IS SENDING THE REQUEST

/* QUERY TO CHECK $_POST DATA WITH: */

/* this is grabbing id that jquery sent over via post */
if(isset($_POST['comment_id'])) {

/* making a variable out of the grabbed id */   
$retreived_comment_id = ($_POST['comment_id']); 

/* this query is bring to frutation the exact id of the comment */
$query = "SELECT * FROM `CysticAirwaves` WHERE `id` = '".$retreived_comment_id."' && `status` = 'active'"; 
$request = mysql_query($query,$connection);
if($result = mysql_fetch_array($request)) {

/* insert the comment into the increment table */

$query = "INSERT INTO `thumb_increment` (
                                `comment_id`
                            ) VALUES (
                            '" . $retreived_comment_id . "'
                                )";
mysql_query($query,$connection);

/* increment the vote in the db */

    }

}


?>

So to summarize what I have left to do:

I need to increment and store the comment in my db, put that in a varaible that is sent back to the page that has the thumb icon on it, and have that variable tack on a increment to the counter

Thanks in advance.

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

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

发布评论

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

评论(1

|煩躁 2024-11-17 03:44:38

编辑

关于数据库部分,根据您的评论...

我只需要包含赞成票的规范化数据库:

comment_id, user_who_voted_id, timestamp

将来如果您想拥有反对票的概念,例如stackoverflow,您可以可能需要添加另一列。在前一种更简单的情况下,要获取计数,您只需执行以下操作:

SELECT count(*) FROM thumb_increment WHERE comment_id = ?

旧答案

在修改状态时不会使用 $.get 进行此类操作。我可能会使用一个帖子。要重写他的示例...

http://api.jquery.com/jQuery.post/

http://api.jquery.com/serialize/

http://api.jquery.com/parent/

// Assuming html that looks something like the following
<form id='comment_details_123123'>
    <input type='hidden' value='123123' name='comment_id' />
    <div class='thumb'><img src='thumb.jpg' /></div>
    <div class='thumb_counter'>3</div>
</form>

$('.thumb').click(function(){
    var comment_form = $(this).parent('form');
    $.post('comment.php', comment_form.serialize(), function(response) {
        comment_form.find('.thumb_counter').html(response.new_count);
    });
});

Edit

Regarding just the database portion, per your comment...

I'd just have the normalized database for upvote contain:

comment_id, user_who_voted_id, timestamp

In the future if you want to have the concept of a downvote, like stackoverflow, you might need to add another column. In the former simpler case, to get the count, you're just doing something like:

SELECT count(*) FROM thumb_increment WHERE comment_id = ?

Old Answer

Wouldn't use $.get for something such as this as you're modifying state. I'd probably use a post. To rewrite his example...

http://api.jquery.com/jQuery.post/

http://api.jquery.com/serialize/

http://api.jquery.com/parent/

// Assuming html that looks something like the following
<form id='comment_details_123123'>
    <input type='hidden' value='123123' name='comment_id' />
    <div class='thumb'><img src='thumb.jpg' /></div>
    <div class='thumb_counter'>3</div>
</form>

$('.thumb').click(function(){
    var comment_form = $(this).parent('form');
    $.post('comment.php', comment_form.serialize(), function(response) {
        comment_form.find('.thumb_counter').html(response.new_count);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文