变量正在失去价值

时间:2014-05-09 23:36:41

标签: php mysql

我有一个朋友系统,在mysql中有一个user_from和一个user_to表。在我的代码中,我有一个脚本,应检查这些表并输出一个应触发操作的按钮。但是,变量$username会丢失其值(即用户个人资料页面的用户名。)

我有一个部分,其中$ username似乎失去了它在代码中标记的值。

if($user == $username) {
    echo "<a href = 'account_settings.php'><p>Edit Your Profile</p></a>";
} else {
    $check_frnd_req = mysql_query("SELECT * FROM friend_requests WHERE (user_from = '$user' AND   user_to = '$username') OR (user_from = '$username' AND user_to = '$user')");
    $frnd_numrows = mysql_num_rows($check_frnd_req);

    if($frnd_numrows == 1) {
        echo "Associate Request Pending";
    } else {
        $select_friends_query = mysql_query("SELECT * FROM friends WHERE '$user' = user_one OR '$user' =     user_two");
        $friend_row = mysql_fetch_assoc($select_friends_query);
        $user_one = $friend_row['user_one'];
        $user_two = $friend_row['user_two'];
        if ($username = $user_one) {
            $addAsFriend = '<input type="submit" class = "frnd_req" name="removefriend" value="Disassociate">';
        } elseif($username = $user_two) {
            $addAsFriend = '<input type="submit" class = "frnd_req" name="removefriend"   value="Disassociate">';
        } else {
            $addAsFriend = '<input type = "submit" class = "frnd_req" name = "addfriend" value = "Send     Associate Request">';
        }
    }
}

//The $username seems to lose its value from here..
if (isset($_POST['addfriend'])) {
    $friend_request = $_POST['addfriend'];

    $user_to = $user;
    $user_from = $username;

    if ($user_to == $username) {
        $errorMsg = "You can't send Assosiate request to yourself!<br />";
    } else {
        $create_request = mysql_query("INSERT INTO friend_requests VALUES ('','$user_to','$user_from')");
        $addAsFriend = "Your Associate Request has been sent!";
    }
// to here
} else {
   //Do nothing
}

echo $addAsFriend;

非常感谢所有帮助。

2 个答案:

答案 0 :(得分:1)

elseif($username = $user_two) {

应该是

elseif($username == $user_two) {

编辑 - 刚刚看到@ Fredd-ii评论,他应该因为提供必要的完整回复而获得赞誉。

答案 1 :(得分:1)

=更改为==。您正在分配值而不使用比较运算符

if ($username == $user_one)

elseif($username == $user_two) {