简体   繁体   English

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

[英]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 . 但是事实证明,SQL INSERT语句的工作量超出了应做的……本来应该与用户account_id保存一行,而喜欢的内容就是screenshot_id But instead, it's inserting the same value multiple times, like this: 但是,它多次插入相同的值,如下所示:

id | id | account_id | account_id | screenshot_id screenshot_id

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

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

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

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

It was just an example, here is the actual code I'm using in php: 这只是一个示例,这是我在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 | id | account_id | account_id | screenshot_id screenshot_id

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

UPDATE 更新

This is the onclick event in the slider jquery plugin: 这是滑块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))
}
})
}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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