用php和jquery实现一个喜欢/不喜欢的计数器 [英] Implementing a like/dislike counter with php and jquery
问题描述
我有一个漫画网站,我想跟踪有多少人对一个漫画ID表示喜欢和不喜欢点击...我想将值存储在数据库中,并报告给我进入浏览器.
I have a comics website, and I'd like to keep track of how many people click like and dislike for a single comic id... I'd like to store the value in my database, and also report it back out to the browser.
这是我无法使用的解决方案:
This is my non-working solution:
viewcomic.php:
<script type="text/javascript">
function likeCounter(choice) {
var site = $("#site").val();
var imgid = $("#imgid").val();
$.get("./scripts/likecounter.php", {_choice : choice, _site : site, _id : imgid},
function(returned_data) {
$("#choice").html(returned_data);
}
);
}
</script>
//Changed to try and work with buttons
<button id="like" onClick="likeCounter(this.id)">Like</button>
<button id="dislike" onClick="likeCounter(this.id)">Dislike</button>
<input id="site" type="hidden" value="<?php echo $site; ?>">
<input id="imgid" type="hidden" value="<?php echo $imgid; ?>">
<br />
Likes: <span id="choice"></span>
likecounter.php
<?php
include 'dbconnect.php';
$site = $_GET['_site'];
$imgid = $_GET['_id'];
$input = $_GET['_choice'];
if ($site == "artwork") {
$table = "comics";
}
else {
$table = "artwork";
}
$likes = $mysqli->query("SELECT like FROM $table WHERE id = $imgid");
$dislikes = $mysqli->query("SELECT dislike FROM $table WHERE id = $imgid");
if ($input == "like") {
$sql = "UPDATE $table SET like = like + 1 WHERE id = $imgid";
$mysqli->query($sql);
$likes++;
else if ($input == "dislike") {
$sql = "UPDATE $table SET like = dislike + 1 WHERE id = $imgid";
$mysqli->query($sql);
$dislikes++;
}
mysqli_close($mysqli);
echo "Likes: " . $likes . ", Dislikes: " . $dislikes;
?>
它没有增加数据库值,也没有向浏览器报告一个值.有什么建议吗?
It's not incrementing the database value, and it's not reporting out a value to the browser. Any suggestions?
谢谢!
推荐答案
技术问题是这一行:
$.get("./scripts/likecounter.php", {_choice : choice, _site : site, _id : id}...
由于未定义网站和ID,您会收到JavaScript错误ReferenceError: site is not defined
as site and id are undefined you'll be getting a javascript error ReferenceError: site is not defined
尝试:
$.get("./scripts/likecounter.php", {_choice : choice, _site : "<?php echo $site; ?>", _id : "<?php echo $imgid; ?>"}...
您不需要隐藏的输入,onClick
也应为onclick
.
You don't need the hidden inputs, also onClick
should be onclick
.
其他快速建议:
确保$ imgid实际上是一个整数:
Make sure $imgid is actually an integer:
$imgid = intval($_GET['_id']);
if($imgid == 0) exit('Invalid id');
以下内容无需分开:
$likes = $mysqli->query("SELECT like FROM $table WHERE id = $imgid");
$dislikes = $mysqli->query("SELECT dislike FROM $table WHERE id = $imgid");
$result = $mysqli->query("SELECT like_count, dislike_count FROM $table WHERE id = $imgid");
$mysqli->query
将仅返回mysqli_result,这意味着您不能只将其回显,而必须进行提取,并且因为您知道只得到一个结果,所以可以使用list($likes, $dislikes) = $result->fetch_array(MYSQLI_NUM);
$mysqli->query
will only return a mysqli_result, meaning you can't just echo it out, you'll have to do a fetch, and because you know you are only getting a single result you can just use list($likes, $dislikes) = $result->fetch_array(MYSQLI_NUM);
已阅读文档以帮助您理解我到底要怎么做.
Have a read of the docs to help you understand what the heck I'm going on about.
正如您提到的那样,人们可以一直喜欢/不喜欢他们想要的东西,您需要使用某种IP日志来限制它.例如,使用以下字段设置一个新的数据库表; ip,表格,imgid.然后,当有人喜欢"登录其IP时:
As you mentioned people can just keep hitting like/dislike all they want, you'll need to limit this with an IP log of some sort. For example setup a new database table with the following fields; ip, table, imgid. Then when someone "likes" log their IP:
$sql = "INSERT INTO xxx (ip, table_name, imgid) VALUES(\"".$_SERVER['REMOTE_ADDR']."\", \"$table\", $imgid)";
$mysqli->query($sql);
然后在添加新的类似项之前检查他们是否有记录:
Then check if they have a record before adding a new like:
$sql = "SELECT ip FROM xxx WHERE ip = \"".$_SERVER['REMOTE_ADDR']."\" AND table_name = "$table" AND imgid = $imgid";
$result = $mysqli->query($sql);
if($result->num_rows == 0) { ...if($input ==... }
这篇关于用php和jquery实现一个喜欢/不喜欢的计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!