简体   繁体   中英

SQL 'INSERT' is inserting the value multiple times instead of only one

Hello guys I was making a like system that sends to database who liked it and the content liked. But turns out that the SQL INSERT statement is doing the job more than it should... It was supposed to save a single line with the user account_id and the content liked that would be the screenshot_id . But instead, it's inserting the same value multiple times, like this:

id | account_id | screenshot_id

1 .|.........2.........|.........15...........

2 .|.........2.........|.........15...........

3 .|.........2.........|.........15...........

4 .|.........2.........|.........15...........

It was just an example, here is the actual code I'm using in 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;
    }
?>

So... How do I make this code insert the values only once like this:

id | account_id | screenshot_id

1 .|.........2.........|.........15...........

UPDATE

This is the onclick event in the slider jquery plugin:

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))
}
})
}

所以...我不确定脚本的其余部分是否正确,但是我在Google上搜索了更多答案,并发现了unbind() ,它对我有用,并加入了onclick函数:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM