关注和取消关注系统 - 不确定我的代码有什么问题

时间:2017-04-10 12:39:26

标签: javascript php sql ajax

我有下面的表格和代码,试图在我的网络应用上建立一个跟随和取消关注的朋友关系。问题在于,当我运行此代码时:

  1. 按钮仍然是"关注"并且"禁用"在Firefox,Chrome,Safari上,它显示"取消关注"在Torch浏览器上。当用户A是登录用户时,会发生这种情况,访问用户-B的个人资料页面
  2. 即使在Torch浏览器上,当我点击"取消关注"或"关注",没有任何反应 - 按钮不会在关注和取消关注之间切换,我的数据库也不会更新。
    注意:我手动输入了"关注"数据库表,因为follow和unfollow按钮不会更新数据库。
  3. TABLENAME - 关注

    id     user1    user2     countrycode mobile       
    1      USER-A   USER-B    234         08023334567
    

    TABLENAME - 用户

    id      username    password     mobile             activated                
     1       USER-A                   08023334567         1
    
     2       USER-B                   08034448987         1
    

    user.php

    <?php
    $following = false;
    $login_username = "USER-A";
    $u = "USER-B";
    $user_ok = true;
    
    if($u != $login_username && $user_ok == true){
        $following_check = "SELECT id FROM follows WHERE user1='$login_username' AND user2='$u' LIMIT 1";
        if(mysqli_num_rows(mysqli_query($db_connect, $following_check)) > 0){
            $following = true;  
        }
    }
    ?><?php
    $follow_button = '<button disabled>Follow</button>';
    
    //LOGIC FOR FOLLOW BUTTON
    if($following == true){
        $follow_button = '<button onclick="followToggle(\'unfollow\',\''.$u.'\',\'followBtn\')">Unfollow</button>';
    } else if($user_ok == true && $u != $log_username && $following == false) {
        $follow_button = '<button onclick="followToggle(\'follow\',\''.$u.'\',\'followBtn\')">Follow</button>';
    } 
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title><?php echo $u; ?></title>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <link rel="stylesheet" href="style/style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script src="js/ajax.js"></script>
    <script>
    function followToggle(type, user, elem) {
        var conf = confirm("Press OK to confirm the '" + type + "' action for user <?php echo $u; ?>.");
        if (conf != true) {
            return false;
        }
        _(elem).innerHTML = 'please wait ...';
        var ajax = ajaxObj("POST", "follow_system.php");
        ajax.onreadystatechange = function() {
            if (ajaxReturn(ajax) == true) {
                if (ajax.responseText == "follow_ok") {
                    _(elem).innerHTML = '<button onclick="followToggle(\'unfollow\',\'<?php echo $u; ?>\',\'friendBtn\')">Unfollow</button>';
                } else if (ajax.responseText == "unfollow_ok") {
                    _(elem).innerHTML = '<button onclick="followToggle(\'follow\',\'<?php echo $u; ?>\',\'friendBtn\')">Follow</button>';
                } else {
                    alert(ajax.responseText);
                    _(elem).innerHTML = 'Try again later';
                }
            }
        }
        ajax.send("type=" + type + "&user=" + user);
    }
    </script>
    </head>
    <body>
    <div id="PageMiddle">
    
        <p><span id="friendBtn"><?php echo $follow_button; ?></p>
    
    </div>
    
    </body>
    </html>
    

    follow_system.php

     <?php
    $user_ok = true;
    $login_username != "";
        if($user_ok != true || $login_username == "") {
            exit();
        }
        ?><?php
        if (isset($_POST['type']) && isset($_POST['user'])){
            $user = preg_replace('#[^a-z0-9._@]#i', '', $_POST['user']);
                $sql = "SELECT COUNT(id) FROM users WHERE username='$user' AND activated='1' LIMIT 1";
                $query = mysqli_query($db_connect, $sql);
                $exist_count = mysqli_fetch_row($query);
                if($exist_count[0] < 1){
                    mysqli_close($db_connect);
                    echo "$user does not exist.";
                    exit();
                }
            if($_POST['type'] == "follow"){
                    $sql = "INSERT INTO follows(user1, user2, countrycode, mobile) VALUES('$login_username','$user','$countrycode','$mobile')";
                    $query = mysqli_query($db_connect, $sql);
                    mysqli_close($db_connect);
                    echo "follow_ok";
                    exit();
                } else if($_POST['type'] == "unfollow"){
                    $sql = "DELETE FROM follows WHERE user1='$login_username' AND user2='$user' AND countrycode='$countrycode' AND mobile='$mobile'";
                    $query = mysqli_query($db_connect, $sql);
                    mysqli_close($db_connect);
                    echo "unfollow_ok";
                    exit();
                } 
            }
    
        ?>
    

0 个答案:

没有答案