我有下面的表格和代码,试图在我的网络应用上建立一个跟随和取消关注的朋友关系。问题在于,当我运行此代码时:
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();
}
}
?>