% # Check to see if all of the required arguments arrived. % unless ($username and $password1 and $password2 and $email_address and $birthdate) % {
Sorry, but your registration information could not be saved because it was missing the following:
Please go back, and fill in the missing information.
% } elsif ($password1 != $password2) {We're sorry, you must enter the same password in both fields.
Please go back, and fill in the missing information.
% } else { <%perl> # Let's see if this username is already taken $sql = "SELECT uid "; $sql .= "FROM Users "; $sql .= "WHERE user_name = ?"; $sth = $dbh->prepare($sql); $result = $sth->execute($username); my ($user_name_taken) = $sth->fetchrow_array; $sth->finish; % if ($user_name_taken) {Sorry, but the username <% $username %> has already been taken by someone else. Please try to register with a different username.
% } else { <%perl> # Since this username is unique, we can # store this user's information in the database $sql = "INSERT INTO users "; $sql .= " (user_name, user_password, user_birthdate, user_homepage, user_email)"; $sql .= " VALUES "; $sql .= " (?, ?, ?, ?, ?)"; $sth = $dbh->prepare($sql); $result = $sth->execute($username, $password1, $birthdate, $homepage, $email_address); $sth->finish; % if ($result) { <%perl> $sql = "SELECT uid "; $sql .= "FROM Users "; $sql .= "WHERE user_name = ?"; $sth = $dbh->prepare($sql); $result = $sth->execute($username); my ($user_name_taken) = $sth->fetchrow_array; $sth->finish; % $session{user_id} = $user_id;Your registration was successful.
Welcome, <% $username %>!
% } else {Sorry, but your registration was unsuccessful.
% } # Checking $result % } # Checking $user_name_taken % } # Ending top-level if/else <%once> my ($dbh, $sql, $sth, $result, $user_id); <%init> $dbh = $m->comp("dbconnect.mcomp"); <%args> $username => undef $password1 => undef $password2 => undef $email_address => undef $birthdate => undef $homepage => undef