Hack 68. Cope with Unexpected Redo
When a user initiates an operation you can ensure that your code copes well with repeated attempts.
Suppose you are writing user account creation routines for a web application. A user can create an account on your system, where the username is his preferred email address. As part of the process, the user will be given a random password if he was successful, or will be shown an error message if appropriate. The form uses USER for username and PASS for password. The username column is the primary key of usertable:
CREATE TABLE usertable ( username varchar(20) primary key, password varchar(20) );
Here is the PHP to provide a user creation form: