| |
// Check if the form has been submitted.
if (isset($_POST['submitted'])) {
require_once ('mysql_connect.php'); // Connect to the db.
// Create a function for escaping the data.
function escape_data ($data) {
global $dbc; // Need the connection.
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string(trim($data), $dbc);
} // End of function.
$errors = array(); // Initialize error array.
// Check for a first name.
if (empty($_POST['first_name'])) {
$errors[] = 'You forgot to enter your first name.';
} else {
$fn = escape_data($_POST['first_name']);
}
// Check for a last name.
if (empty($_POST['last_name'])) {
$errors[] = 'You forgot to enter your last name.';
} else {
$ln = escape_data($_POST['last_name']);
}
// Check for an email address.
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address.';
} else {
$e = escape_data($_POST['email']);
}
// Check for a password and match against the confirmed password.
if (!empty($_POST['password1'])) {
if ($_POST['password1'] != $_POST['password2']) {
$errors[] = 'Your password did not match the confirmed password.';
} else {
$p = escape_data($_POST['password1']);
}
} else {
$errors[] = 'You forgot to enter your password.';
}
if (empty($errors)) { // If everything's okay.
// Register the user in the database.
// Check for previous registration.
$query = "SELECT nuid FROM newsletteruser WHERE email='$e'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
// Make the query.
$query = "INSERT INTO newsletteruser (first_name, last_name, email, password, registerdate) VALUES ('$fn', '$ln', '$e', '$p', NOW() )";
$result = @mysql_query ($query); // Run the query.
if ($result) { // If it ran OK.
// Send an email, if desired.
// Print a message.
echo 'Thank you!
You are now registered. In Chapter 9 you will actually be able to log in!
';
// Include the footer and quit the script (to not show the form).
include ('./includes/footer.html');
exit();
} else { // If it did not run OK.
echo 'System Error
You could not be registered due to a system error. We apologize for any inconvenience. '; // Public message.
echo '' . mysql_error() . '
Query: ' . $query . ' '; // Debugging message.
include ('./includes/footer.html');
exit();
}
} else { // Already registered.
echo 'Error!
The email address has already been registered. ';
}
} else { // Report the errors.
echo 'Error!
The following error(s) occurred: ';
foreach ($errors as $msg) { // Print each error.
echo " - $msg \n";
}
echo ' Please try again.
';
} // End of if (empty($errors)) IF.
mysql_close(); // Close the database connection.
} // End of the main Submit conditional.
?>
|
|
|