繁体   English   中英

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

[英]How do I store incremented votes in my database via AJAX?

我正处于我的第一个 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 */

    }

}


?>

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

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

提前致谢。

编辑

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

我只需要将标准化数据库用于 upvote 包含:

comment_id, user_who_voted_id, timestamp

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

SELECT count(*) FROM thumb_increment WHERE comment_id = ?

旧答案

在您修改 state 时,不会将 $.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);
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM