Php 如何通过AJAX在数据库中存储递增的投票?
我正处于我的第一个AJAX项目的最后阶段。我有一个有评论的社交网络。我只是添加了一个拇指图标,当按下时,它会通过JQUERY将注释的id发送到后台php页面,该页面应该存储注释的id以及被按下的记录。在这之后,我需要它将该记录发送回拇指图标所在的页面,并告诉该页面拇指已被击中,并在指定区域增加计数器 在thumb_incrementtable中,我有: 自动递增且 comment_id,是正在被提升投票的评论的id 我想知道我是否应该添加另一个列来保存追加投票的数量,或者只是在comment_id列中进行跟踪。我对那里的逻辑有点困惑 到目前为止,我可以点击拇指,让它从我的另一个页面发送id,并通过$u POST获取id。这是代码:Php 如何通过AJAX在数据库中存储递增的投票?,php,jquery,html,ajax,auto-increment,Php,Jquery,Html,Ajax,Auto Increment,我正处于我的第一个AJAX项目的最后阶段。我有一个有评论的社交网络。我只是添加了一个拇指图标,当按下时,它会通过JQUERY将注释的id发送到后台php页面,该页面应该存储注释的id以及被按下的记录。在这之后,我需要它将该记录发送回拇指图标所在的页面,并告诉该页面拇指已被击中,并在指定区域增加计数器 在thumb_incrementtable中,我有: 自动递增且 comment_id,是正在被提升投票的评论的id 我想知道我是否应该添加另一个列来保存追加投票的数量,或者只是在comment_i
<?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 */
}
}
?>
因此,总结一下我剩下的工作:
我需要增加注释并将其存储在我的数据库中,将其放在一个变量中,该变量被发送回有拇指图标的页面,并将该变量tack以增量的形式添加到计数器中
提前感谢。编辑
关于数据库部分,根据您的评论
我只希望upvote的规范化数据库包含:
comment_id, user_who_voted_id, timestamp
在将来,如果您想要像stackoverflow一样拥有向下投票的概念,您可能需要添加另一列。在前一种更简单的情况下,要获得计数,您只需执行以下操作:
SELECT count(*) FROM thumb_increment WHERE comment_id = ?
旧答案
在修改状态时,不会使用$.get进行类似的操作。我可能会用一个帖子。改写他的例子
jquery部分对我来说工作得很好。我需要在php页面上的帮助,如我的问题中所述。
// 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);
});
});