dongyinpan9284 2015-10-31 10:29
浏览 277
已采纳

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

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))
}
})
}
  • 写回答

1条回答 默认 最新

  • douzhong3887 2015-10-31 11:18
    关注

    So... I'm not sure if the rest of the script is correct but I went deeper on google for more answers and found out the unbind() and it worked for me putting in the onclick function:

    o.unbind().on("click",".vote",function(e){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题