跟随按钮将无法工作。可能的jQuery.ajax,PHP或数据库问题

时间:2020-11-12 20:20:13

标签: php jquery ajax mysqli

我知道这个问题与这段代码有关,但我不知道是什么。我尝试过试验无济于事。我觉得它与ajax有关,但不确定我不太熟悉它。

index.php

</style>
<script>
    function follow_update(id) {
        var cur_count = jQuery('.follow_loop_'+id).html();
        cur_count++
        jQuery('.follow_loop_'+id).html(cur_count);
        jQuery.ajax({
            url:'update_followers.php',
            type:'post',
            data:'type=like&user_id='+id,
            success:function(result){
            }
        })
    }     
</script>
<a href="javascript:void(0)" class="" id="follow_btn">
    <span onclick="follow_update('<?php echo $profiles['user_id']?>')">
        <span class="follow_loop_<?php echo $profiles['user_id']?>" id="follow_count"><?php echo $stats['total_followers']?></span>
    </span>
</a>

update_followers.php

<?php

include('config.php');
session_start();
$id = mysqli_real_escape_string($conn, $_GET['user_id']);
$get_if_already_followed = mysqli_query($conn, "SELECT followers FROM follower WHERE followers = '".$_SESSION['username']."' AND user_id = '".$id."' ");
$if_already_followed = mysqli_fetch_assoc($get_if_already_followed);
$get_active = mysqli_query($conn, "SELECT active FROM users WHERE user_id = '".$_SESSION['user_id']."' ");
$active = mysqli_fetch_assoc($get_active);

if ($active['active'] == 1 && $if_already_followed === '') {
    $sql = "INSERT INTO follower (user_id, followers) VALUES ('".$id."', '".$_SESSION['username']."')";
    mysqli_query($conn, $sql);

    $sql = "UPDATE users SET total_followers=total_followers+1 WHERE user_id=$id";
    mysqli_query($conn, $sql); 
} else {
    if ($active['active'] == 1 && $if_already_followed !== '') {
        $sql = "DELETE FROM follower WHERE user_id = '".$id."' AND followers = '".$_SESSION['username']."' ";
        mysqli_query($conn, $sql);

        $sql = "UPDATE users SET total_followers=total_followers-1 WHERE user_id=$id";
        mysqli_query($conn, $sql); 
    }
}

?>

1 个答案:

答案 0 :(得分:0)

我认为这是问题所在

$if_already_followed === ''

您正在检查$if_already_followed === '',但是如果未找到结果,则mysql_fetch_assoc返回null而不是空字符串。您应该按照https://www.php.net/manual/en/mysqli-result.fetch-assoc.php

检查是否为null
豫ICP备18024241号-1