0

I have the table and the codes below, trying to build a follow and unfollow friend relationship on my web app. The issue is that when I run this code:

  1. The button remains "Follow" and "disabled" on Firefox, Chrome, Safari, while it shows "Unfollow" on Torch browser. This happen when user-A who is the login user visit the profile page of user-B
  2. Even on Torch browser, when I clicked on "Unfollow" or "Follow", nothing happens - The button does not toggle between follow and unfollow, and my DB is not updated.
    NOTE: I manually entered the values in the "follow" DB table since the follow and unfollow buttons are not updating the DB.

TABLENAME - follows

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

TABLENAME - users

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();
            } 
        }

    ?>
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 10 '17 at 12:40
  • Have you checked your error logs? – Jay Blanchard Apr 10 '17 at 12:40
  • @Jay Blanchard, I will sort out those later. Thanks. It's not an oversight. I mean the SQL Injection attack. – Adébáyò Òjó Apr 10 '17 at 12:48
  • 2
    I hate when people say *"I'm not that far along..."* or *"This site will not be public..."* or *"It's only for school, so security doesn't matter..."*. If teachers and professors are not talking about security from day one, they're doing it wrong. Challenge them. They're teaching sloppy and dangerous coding practices which students will have to unlearn later. I also hate it when folks say, *"I'll add security later..."* or *"Security isn't important now..."* or *"Ignore the security risk..."*. If you don't have time to do it right the first time, when will you find the time to add it later? – Jay Blanchard Apr 10 '17 at 12:48
  • My error logs shows logs of days ago. No recent log that shows what's happening. – Adébáyò Òjó Apr 10 '17 at 13:09
  • Have you checked your browser's console for JS errors? – Jay Blanchard Apr 10 '17 at 13:11
  • Uncaught TypeError: Cannot set property 'innerHTML' of null at followToggle (VM46 user.php?u=USER-B:50) at HTMLButtonElement.onclick (user.php?u=USER-B:80) – Adébáyò Òjó Apr 10 '17 at 13:28
  • Please don't move it. I'm not yet qualified to view chat. – Adébáyò Òjó Apr 10 '17 at 13:48
  • That means `_(elem)` is not set. – Jay Blanchard Apr 10 '17 at 13:53

0 Answers0