我有一个漫画网站,我想跟踪有多少人点击喜欢和不喜欢单个漫画 ID...我想将值存储在我的数据库中,并将其报告回浏览器。
这是我的非工作解决方案:
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>
像counter.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;
?>
它不会增加数据库值,也不会向浏览器报告值。有什么建议么?
谢谢!