大家好,我正在制作一个类似的系统,将喜欢它和喜欢的内容发送到数据库。但事实证明,SQL INSERT语句所做的工作超出了它应该做的。。。它应该与用户account_id
保存一行,并且喜欢的内容将是screenshot_id
。但相反,它多次插入相同的值,如下所示:
id | account_id |屏幕截图_id
1.|。。。。。。。。。2…………|………15………
2.|。。。。。。。。。2…………|………15………
3.|。。。。。。。。。2…………|………15………
4.|。。。。。。。。。2…………|………15………
这只是一个例子,下面是我在php:中使用的实际代码
<?php
include('connect.php');
$acc_id = $_POST['acc_id']; //value = 2
$id = $_POST['id']; //value = 15
if($id && $acc_id != 0){
$count = mysqli_num_rows(mysqli_query($connect, "SELECT `id` FROM `screenshot_votes` WHERE `account_id` = '$acc_id' AND `screenshot_id` = '$id';"));
if($count == 0){ //checking if there is already a vote with those values
mysqli_query($connect, "INSERT INTO `screenshot_votes` (id,account_id,screenshot_id,vote) VALUES (NULL,'$acc_id','$id','2');");
//values being insert above
}
$row = mysqli_num_rows(mysqli_query($connect, "SELECT `vote` FROM `screenshot_votes` WHERE `screenshot_id` = $id AND `vote` = 2;"));
echo $row;
}
?>
所以。。。我如何让这个代码只插入一次值,如下所示:
id | account_id |屏幕截图_id
1.|。。。。。。。。。2…………|………15………
更新
这是滑块jquery插件中的onclick事件:
j=function(){
var acc_id = r.accid;
e.each(o,function(t,n){
var r=e(n).children("img:first-child").attr("views");
r||(r=e(n).children("a").find("img:first-child").attr("views"));
o.on("click",".vote",function(e){
var id = $(this).attr('id');
var name = $(this).attr('name');
var dataString = 'id=' + id;
if(name == 'up'){
$('.pos_value.id'+id).fadeIn(100).html('...');
$.ajax({
type: 'POST',
url: 'pages/scripts/up_vote.php',
data: {id: id, acc_id: acc_id},
cache: false,
success: function(html){
$('.pos_value.id'+id).html(html);
$('.vote.pos_vote_enabled.img'+id).css({"background-image": "url(images/icons/pos.png)"});
$('.vote.pos_vote_enabled.img'+id).attr('class', 'pos_vote');
$('.vote.neg_vote_enabled.img'+id).attr('class', 'neg_vote');
}
});
}else{
$('.neg_value.id'+id).fadeIn(100).html('...');
$.ajax({
type: 'POST',
url: 'pages/scripts/down_vote.php',
data: {id: id, acc_id: acc_id},
cache: false,
success: function(html){
$('.neg_value.id'+id).html(html);
$('.vote.neg_vote_enabled.img'+id).css({"background-image": "url(images/icons/neg.png)"});
$('.vote.neg_vote_enabled.img'+id).attr('class', 'neg_vote');
$('.vote.pos_vote_enabled.img'+id).attr('class', 'pos_vote');
}
});
}
return false;
});
if(r){
r=e('<span class="bjqs-views">'+r+'</span>');
r.appendTo(e(n))
}
})
}
所以。。。我不确定脚本的其余部分是否正确,但我在谷歌上搜索了更多答案,找到了unbind()
,它对我输入onclick函数起到了作用:
o.unbind().on("click",".vote",function(e){