SQL' INSERT'是多次插入值而不是只插入一次

时间:2015-10-31 10:29:19

标签: php sql insert

大家好,我正在制作一个类似的系统,发送给喜欢它并喜欢的内容的数据库。但事实证明,SQL INSERT语句正在执行的工作超出了应有的范围......它应该与用户account_id保存一行,并且喜欢的内容将是screenshot_id。但相反,它会多次插入相同的值,如下所示:

id | account_id | screenshot_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 | screenshot_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))
}
})
}

1 个答案:

答案 0 :(得分:0)

所以...我不确定脚本的其余部分是否正确但是我在google上更深入了解更多答案并找到unbind()并且它对我来说是有用的onclick功能:< / p>

o.unbind().on("click",".vote",function(e){