I've been working on handcoding some forums for my website for a long time now, and had previously given up (temporarily) because I couldn't get the form to write to the database. Now that I've got that working, I have encountered another error.
The registration script does a few things (well, it should):
1. Take the information from the form
2. Check to see if the username already exists
2a. If it does, prompt the user to try again
2b. If it is unique, continue to step 3
3. Input the information to the database
4. Send the user an email with their username and password
The problem is that the script won't check to see if the username already exists. This could result in 20+ people with the same username, and I definately don't want that. Mass confusion would result. It does everything else, though.
No one that I know seems to see a problem with this script, so I turn to you guys for help (not as a last resort, of course, simply a forgotten resource. :-D)
reg.php
PHP Code:
<?php
// inputting registration data
echo "<b>Register an Account</b>";
echo "<br><br><br>By signing up for the Inevitable Forums, you hereby agree not to use these forums to discuss any illegal activities whatsoever, including, but not limited to, warez, hacking and drug dealing / using. You also agree not to post or discuss pornography.<br><br><br>";
echo "<form method=post action=/dev/test/reg2.php>";
echo "Username: <input name=uname type=text id=uname><br>";
echo "Password: <input name=pword type=password id=pword><br>";
echo "eMail Address: <input name=email type=text id=email><br>";
echo "<input type=submit value=Join!>";
echo "</form><br><br><br>";
?>
reg2.php
PHP Code:
<?php
$result=mysql_num_rows("SELECT user_name FROM forum_users WHERE user_name=$_GET[uname]");
// if username is already in use, display error
if($result!=0)
echo("Sorry, but the username \"".$_GET[uname]."\"is already in use. Please hit your browser's back button and try a new name.");
// get info from form
$uname = $_GET['uname'];
$pword = $_GET['pword'];
$email = $_GET['email'];
// remove characters that may cause an error
$uname = stripslashes($uname);
$pword = stripslashes($pword);
$email = stripslashes($email);
// prepare password for database
$dbpword = md5($pword);
// inserting into the database and displaying a "thank you" message
mysql_query("INSERT INTO `forum_users` (user_name,user_pword,user_email,user_regdate) VALUES ('$uname','$dbpword','$email','now()')");
// information for email to new user
$subject = 'theInevitable Forums Account Registration';
$message = "Greetings $uname,
This email has been sent to notify you of your account details. Please keep this information in a safe location!
Username: $uname
Password: $pword
To log in to your account, head to http://theinevitable.org/forum.php?type=login
Thank you and enjoy your stay,
Trespasser";
// send the email
mail($email,$subject,$message,"From: Trespasser <trespasser@theinevitable.org>\nX-Mailer: PHP/" . phpversion());
?>
Any ideas?