This is happening because MySQL reads the privileges once at startup of the MySQL daemon and stores that information in memory. Hence, when changes are made to the mysql.user table, they are not immediately recognized.
Probably the easiest way to get around this is to connect to the database as 'root' when changing MySQL passwords and use GRANT and REVOKE statements (rather than UPDATE) when changing MySQL user passwords. This (at least according to the MySQL manual) will allow the changes to become effective immediately.
Then re-send the header, and you should be able to connect to the MySQL server upon refresh with the username and new password.
Alternatively, you should be able to run:
mysqladmin -u root -proot password reload
inside a system() or exec() call and acheive the same effect.
Either way, you must close the MySQL connection and re-connect to the server using the new password.
But the question that just begs to be asked is, why are you using multiple user names and passwords to start with...? I've always found it easier to use a single database user name and password (with strictly limited permissions) for an entire application, and then create a user table which the application uses, to avoid this type of scenario...