使用php和ajax添加friends脚本


Adding friends script with php and ajax

我制作了一个简单的脚本,允许用户添加另一个用户作为他们的朋友。到目前为止,一切如预期。用户登录到自己的帐户,然后浏览另一个用户的配置文件页面,有一个按钮add as friend,用户可以添加它。这是通过ajax/php/mysql完成的。

目前,只有当用户添加另一个用户为好友时,以及当他们已经是好友时,我才成功地向用户显示消息。但当他试图添加自己时,他收到了消息You're now friends。这里是php部分,用于检查用户是否在他的朋友列表中。如果是,他会得到他们是朋友的信息,如果不是,他会收到他们现在是朋友的消息。

if(!empty($_POST["id"])) {
    $friend_id = $_POST['id'];             
    $user_id = $_SESSION['id'];   
            $value = $pdo->prepare('SELECT * FROM user_friends WHERE friend_id= ? and user_id = ?');
            $value->bindParam(1, $friend_id, PDO::PARAM_INT);
            $value->bindParam(2, $user_id, PDO::PARAM_INT);             
            $value->execute();
            $result = $value->fetch();    
     if( $result > 0) {
        echo 'Already friends';         
     }
     else 
     {
        if ( $_SESSION['id'] != $_POST['id'] ){
            $friend_id = $_POST['id'];             
            $user_id = $_SESSION['id']; 
            $query = $pdo -> prepare("INSERT INTO user_friends (friend_id, user_id, friendsSince)
                                  VALUES (:friend_id, :user_id, NOW())");   
            $query -> execute(array(
                          ":friend_id"  => $friend_id,
                          ":user_id"    => $user_id,
            ));     
     } 
else 
{ 
   echo ''; 
}
}

这是将$id发送到脚本的ajax部分

    $('.newFriend, .buttons').click(function(){
    $.post('misc/add_friend.php', 
    { 
        "id": $(this).attr('id'),
    },
    function(data){
        if(data == 0){
            $('#message_newfriend').html('<div id="alertFadeOut" style="color: green">Already friends!</div>');
             $('#alertFadeOut').fadeOut(3000, function () {
                $('#alertFadeOut').text('');
             }); 
        }           
        else {
             $('#message_newfriend').html('<div id="alertFadeOut" style="color: red">You're now friends!</div>');
             $('#alertFadeOut').fadeOut(3000, function () {
                $('#alertFadeOut').text('');
             }); 
        }
     });
});

问题是当当前用户点击按钮并尝试将自己添加为好友时。脚本检查表中的ID,看看它是否未存储,然后脚本将插入该用户,当前用户收到消息,他成功地将该用户添加为好友。

我不知道如何向他表明他不能添加自己的信息。有人能帮我做这个吗?

您只需要从PHP为每个条件返回一条特定的消息,我为用户尝试添加自己时添加了一个新的响应,为用户成功添加时添加了另一个响应。然后使用JS检查响应。

PHP:

if (!empty($_POST["id"])) {
    $friend_id = $_POST['id'];
    $user_id   = $_SESSION['id'];
    $value = $pdo->prepare('SELECT * FROM user_friends WHERE friend_id= ? and user_id = ?');
    $value->bindParam(1, $friend_id, PDO::PARAM_INT);
    $value->bindParam(2, $user_id, PDO::PARAM_INT);
    $value->execute();
    $result = $value->fetch();
    if ($result > 0) {
        echo 'Already friends';
    } else {
        if ($_SESSION['id'] != $_POST['id']) {
            $friend_id = $_POST['id'];
            $user_id   = $_SESSION['id'];
            $query = $pdo->prepare("INSERT INTO user_friends (friend_id, user_id, friendsSince)
                                  VALUES (:friend_id, :user_id, NOW())");
            $query->execute(array(
                    ":friend_id" => $friend_id,
                    ":user_id" => $user_id
            ));
            echo 'Added as friend';
        } else {
            echo 'Trying to add themselves';
        }
    }
}

JS-

$('.newFriend, .buttons').click(function() {
    $.post('misc/add_friend.php',
            {
                "id": $(this).attr('id')
            },
            function (response) {
                switch (response) {
                    case 'Already friends':
                        $('#message_newfriend').html('<div id="alertFadeOut" style="color: green">Already friends!</div>');
                        $('#alertFadeOut').fadeOut(3000, function () {
                            $('#alertFadeOut').text('');
                        });
                        break;
                    case 'Trying to add themselves':
                        $('#message_newfriend').html('<div id="alertFadeOut" style="color: red">You''re trying to add yourself</div>');
                        $('#alertFadeOut').fadeOut(3000, function () {
                            $('#alertFadeOut').text('');
                        });
                        break;
                    case 'Added as friend':
                        $('#message_newfriend').html('<div id="alertFadeOut" style="color: red">You''re now friends!</div>');
                        $('#alertFadeOut').fadeOut(3000, function () {
                            $('#alertFadeOut').text('');
                        });
                        break;
                }
            });
});

首先,您不应该在用户自己的名称旁边显示添加为好友按钮。

但为了解决你的问题,张贴

if(!empty($_POST["id"])) {
    $friend_id = $_POST['id'];             
    $user_id = $_SESSION['id'];   
    if ($user_id === $friend_id) {
        echo ''; 
    }
    else {
            $value = $pdo->prepare('SELECT * FROM user_friends WHERE   friend_id= ? and user_id = ?');
            $value->bindParam(1, $friend_id, PDO::PARAM_INT);
            $value->bindParam(2, $user_id, PDO::PARAM_INT);             
            $value->execute();
            $result = $value->fetch();    
     if( $result > 0) {
        echo 'Already friends';         
     }
     else 
     {
        if ( $_SESSION['id'] != $_POST['id'] ){
            $friend_id = $_POST['id'];             
            $user_id = $_SESSION['id']; 
            $query = $pdo -> prepare("INSERT INTO user_friends (friend_id, user_id, friendsSince)
                                  VALUES (:friend_id, :user_id, NOW())");   
            $query -> execute(array(
                          ":friend_id"  => $friend_id,
                          ":user_id"    => $user_id,
            ));     
        } 
       else 
       { 
           echo ''; 
    }
  }
}