Use parenthesis to define what you mean with the AND and OR statements.
Right now the query will match all records:
1. where id=$id AND firstname=$name
2. where lastname=$name.
So you also get all records where lastname=$name but id is not $id.
What you want is:
WHERE id=$id AND (firstname=$name OR lastname=$name)
Further note:
Are you sure you want to match on the id?
ID's are generally used to identify records. They are unique for each record, so if id=$id, then you have already specified one record. If you specify that that record must contain a certain name, then how did you ge the $id in the first place?