Jump to content

Registration does not work! 2 UNIQUE keys!


angelali

Recommended Posts

I made a registration form where I put both email address and username as UNIQUE key in database. I did that to preent duplicate email address and username to enter, and also to check if they have already registered, if yes, then the user will have to choose another email or username.

 

I always do registration form where email address is only the UNIQUE key, but today, I'm putting both email address and username as UNIQUE. However, when I register, it gives me my error message to display when either the email address or username has already registered. Here my from below:

 

<form action="register.php" method="post">
<table width="760">
<tr>
<td><label>Your email address:</label></td>
<td><input type="email" required size="45" maxlength="50" name="email"/></td>
</tr>
<tr>
<td><label>Confirm your email address:</label></td>
<td><input type="email" required size="45" maxlength="50" name="cemail"/></td>
</tr>
<tr>
<td><label>Insert a nickname:</label></td>
<td><input type="text" required size="45" maxlength="32" name="nick"/></td>
</tr>
<tr>
<td><label>Insert a password:</label></td>
<td><input type="password" required size="45" maxlength="32" name="pass"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Register"/></td>
</tr>
</table>
</form>

 

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['email']) && isset($_POST['cemail']) && isset($_POST['nick']) && isset($_POST['pass'])) {

$email = mysql_real_escape_string(strip_tags(trim($_POST['email'])));
$cemail = mysql_real_escape_string(strip_tags(trim($_POST['cemail'])));
$nick = mysql_real_escape_string(strip_tags(trim($_POST['nick'])));
$pass = mysql_real_escape_string(strip_tags(trim($_POST['pass'])));

if (empty($email) || empty($cemail) || empty($nick) || empty($pass)) {
echo 'All fields marked with an asterisk are required!';    
}  else if (strlen($nick) > 32){
echo 'Your nickname cannot be more than 32 characters!';        
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL) || !filter_var($cemail, FILTER_VALIDATE_EMAIL)) {
echo 'Your email address is invalid!';        
} else if (strlen($email) > 50){
echo 'Your email address cannot be more than 50 characters!';        
} else if ($email !== $cemail) {
echo 'Your email address does not match!';        
} else if (strlen($pass) > 32) {
echo 'Your password cannot be more than 32 characters!';    
} else {

$con = @mysql_connect ('localhost', 'root', '') or die ('A problem has occurred,!');    
@mysql_select_db ('db', $con) or die ('A problem has occurred!');
$pen = md5($pass);
$register = "INSERT INTO reg (reg_ID, email, nickname, password) VALUES ('NULL', '$email', '$nick', $pen')";
mysql_query($register, $con);
if (mysql_errno ($con) > 0) {
echo 'Either the email address or the username has already taken!';    
} else {
echo "Registration Successful like a boss!";
mysql_close($con);    
}
}
}
}
?> 

 

Well, each time I choose to make anew registration when choosing a new email address and nickname, I got this error: Either the email address or the username has already taken!...

Link to comment
Share on other sites

The way I handle this is a on submit I just check if the username and email exist in the database with just a database query that if it returns 0 then they are valid.

<?php
if (!isset($_POST['register']))
						{

						?>
							<div id="formContainer">
								<h2>Register</h2>
								<form action="?action=register" method="post">
									<table>
										<tr>
											<td>Username </td>
											<td><input type="text" name="username" /></td>
										</tr>
										<tr>
											<td>Password </td>
											<td><input type="password" name="password" /></td>
										</tr>
										<tr>
											<td> </td>
											<td><input type="password" name="passconf" /></td>
										</tr>
										<tr>
											<td>Email</td>
											<td><input type="text" name="email"/></td>
										</tr>
										<tr>
											<td> </td>
											<td><input type="text" name="emailconf" /></td>
										</tr>
									</table>
									<table>
									<tr>
											<td>Birth Date </td>
											<td>
												<select name="day">
													<option value="01">1</option>
													<option value="02">2</option>
													<option value="03">3</option>
													<option value="04">4</option>
													<option value="05">5</option>
													<option value="06">6</option>
													<option value="07">7</option>
													<option value="08">8</option>
													<option value="09">9</option>
													<option value="00">10</option>
													<option value="11">11</option>
													<option value="12">12</option>
													<option value="13">13</option>
													<option value="14">14</option>
													<option value="15">15</option>
													<option value="16">16</option>
													<option value="17">17</option>
													<option value="18">18</option>
													<option value="19">19</option>
													<option value="20">20</option>
													<option value="21">21</option>
													<option value="22">22</option>
													<option value="23">23</option>
													<option value="24">24</option>
													<option value="25">25</option>
													<option value="26">26</option>
													<option value="27">27</option>
													<option value="28">28</option>
													<option value="29">29</option>
													<option value="30">30</option>
													<option value="31">31</option>
												</select>

												<select name="month">
													<option value="January">January</option>
													<option value="Februrary">February</option>
													<option value="March">March</option>
													<option value="April">April</option>
													<option value="May">May</option>
													<option value="June">June</option>
													<option value="July">July</option>
													<option value="August">August</option>
													<option value="September">September</option>
													<option value="October">October</option>
													<option value="Novemeber">November</option>
													<option value="December">December</option>
												</select>

												<input type="text" style="width: 225px;" name="year" />
												</td>
										</tr>
									</table>
									<table>
										<tr>
											<td><input type="submit" name="register" value="Register" /></td>
										</tr>
									</table>
								</form>
							</div>
						<?php
							}
							else
							{
								//Declare Variables
								$username = secure($_POST['username']);
								$password = secure($_POST['password']);
								$passconf = secure($_POST['passconf']);
								$email = secure($_POST['email']);
								$emailconf = secure($_POST['emailconf']);
								$day = secure($_POST['day']);
								$month = secure($_POST['month']);
								$year = secure($_POST['year']);
								$birthDate = $month . ' ' . $day . ', ' . $year;
								$regDate = date("m/d/Y");

								//Create the Activation Code
								$activationCode = mt_rand(10000, 99999);
								$activationCode = sha1($activationCode . $username);

								if (!$username || !$password || !$email || !$emailconf || !$day || !$month || !$year || !$birthDate || !$regDate)
								{
									echo 'Please completely fill out the form';
								}
								else
								{
									$query = mysql_query("SELECT * FROM `users` WHERE username='$username'");

									 if (($query = mysql_num_rows($query)) > 0) {
						                echo 'Username Unavailable';
						            } else {
						            	
						            	$query = mysql_query("SELECT * FROM `users` WHERE email='$email'");
						            
						            	if (mysql_num_rows($query) > 0)
						            	{
						            		echo 'Email is already in our database';
						            	}
						            	else
						            	{
						            		if (!$password == $passconf)
						            		{
						            			echo 'Passwords do not match';
						            		}
						            		else
						            		{
						            			//users birthday, ideally you would take this data from a form
												$user_bd = strtotime($birthDate);

												//Age requirement, using 15 as the minimum age required
												$age_req = strtotime('+15 years', $user_bd);

												//Grab the current UNIX timestamp and compare it to the minimum required
												if(time() < $age_req){
													echo 'You must be at least 15 years of age to join our site';
												}
												else
												{
													if (!$email == $emailconf)
													{
														echo 'Emails do not match';
													}
													else
													{
														$encpass = sha1($password . SALT);

														$query = "INSERT INTO `users` (username, password, email, birthday, activationCode, regDate) VALUES('$username', '$encpass', '$email', '$birthDate', '$activationCode', '$regDate' )";

														if (mysql_query($query))
														{
															$to = $email;
															$subject = 'Activate your account D&I Photo!';
															$message = 'Hey!
															We are sending you this message because you have created an account on dandiphoto.ca! Below is a link to activate your account

															' . SITE_URL . '/index.php?action=activate&code=' . $activationCode . '>If the link does not work go to ' . SITE_URL . '/index.php?action=activate< and enter in your code below

															' . $activationCode . '


															Thanks, D&I Photo';
															$from = "noreply@dandiphoto.ca";
															$headers = "From: " . $from . "\r\n";
															$headers .= "Reply-To: ". $from . "\r\n";
															$headers .= "MIME-Version: 1.0\r\n";
															$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

															mail($to,$subject,$message,$headers);

															echo '<br /><br/>';
														?>
															<h2>Check Your Email!</h2>
															<p>We have sent an activation email to the address below. If you don't see it in your inbox, please check your spam folder.</p>
															<span id="email"><?=$email?></span>
														<?
														}
														else
														{
															echo 'User not Created';
														}
													}
												}
						            		}
						            	}
						            }
								}
							}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.