如何通过 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 */
}
}
?>
所以总结一下我剩下要做的事情:
我需要增加评论并将其存储在我的数据库中,将其放入一个变量中,该变量发送回带有拇指图标的页面,然后将其存储在数据库中。可变钉在计数器上的增量
提前致谢。
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_increment
table I have:
id
that is auto incremented andcomment_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑
关于数据库部分,根据您的评论...
我只需要包含赞成票的规范化数据库:
将来如果您想拥有反对票的概念,例如stackoverflow,您可以可能需要添加另一列。在前一种更简单的情况下,要获取计数,您只需执行以下操作:
旧答案
在修改状态时不会使用 $.get 进行此类操作。我可能会使用一个帖子。要重写他的示例...
http://api.jquery.com/jQuery.post/
http://api.jquery.com/serialize/
http://api.jquery.com/parent/
Edit
Regarding just the database portion, per your comment...
I'd just have the normalized database for upvote contain:
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:
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/