Should user IDs be used instead of usernames for tracking last login dates in a community application?

Using user IDs instead of usernames for tracking last login dates in a community application is more efficient and secure. User IDs are unique and do not change, unlike usernames which can be edited by users. Storing and querying based on user IDs will result in faster database operations and better data integrity.

// Assuming $userId is the user ID of the logged in user
$lastLoginDate = date('Y-m-d H:i:s');
$updateQuery = "UPDATE users SET last_login = '$lastLoginDate' WHERE id = $userId";
// Execute the query to update the last login date in the database