在PHP中更改变量的逻辑问题

I'm a total noob when it comes to PHP, as I'm sure you'll discover once you read this question.

I'm messing around and trying to learn, and I've made a chat room using PHP. I'm trying to add functionality so that a user can change which room they are in by using a drop-down box with some pre-defined rooms. I have a login page which asks for a username, password, and which room (the dropdown box). The login page then directs to the index.php page which is where the rest of the chat code is at. The index page has a section that checks to see if the user has changed rooms. If they have changed, it displays a message in the OLD room to say that they've left, and a welcome message in the NEW room.

The issue that I've run into is that when I log in, the room variable gets broken somehow. It puts me in a "blank" room. If I use the drop down box after that to change rooms, everything works fine. I'm just having issues with the initial login.

I tried removing the change room "routine" completely, and I'm able to log in just fine (but I can't switch rooms, obviously) -- so that leads me to believe that there's a logic error somewhere here.

Sorry for the long-winded description - I'm new here and I don't know how much info to provide.

Here's the code that I have that checks/changes rooms -- anyone have any recommendations?

    //check for room change
if(!($_SESSION['room'] == $_POST[r])){
$query = "INSERT INTO roomdata (message, room) VALUES (' * $_SESSION[username] ($flags$ip$flags) has joined the room. Welcome!!', '$_POST[r]'), ('* $_SESSION[username] has left the room.','$_SESSION[room]')";
$result = mysql_query($query);
$_SESSION['room'] = $_POST['r'];
}

Again, I'm new to PHP and I'm sure my code is pretty crappy. I'm also aware that mysql is depreciated, but I'm just doing this for a hobby and I'll learn mysqli once I've got this sorted out.

Thank you in advance to anyone who has time to help!