REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
Add Unique index to the username column;
REPLACE INTO user SET username=".$username." AND password=".$password;