Problem with register system

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JWayne93
Forum Newbie
Posts: 1
Joined: Sun Dec 15, 2013 4:59 pm

Problem with register system

Post by JWayne93 »

I have been coding a register system, I have several IF ELSE statements and when I upload my php file onto my hosting website and enter all the user data I am then shown the message saying "an error occurred while signing up" but I cannot see where I went wrong? thanks

Code: Select all

<?php
  include('config.php');
 ?>
  <?php
  //check if the form has been sent
  if(isset($_POST['username'], $_POST['password'], $_POST['passverify'], $_POST['emailAddress'], $_POST['gender'],
   $_POST['relationshipStatus'], $_POST['country'], $_POST['postCode'], $_POST['mobileNumber'], $_POST['dateOfBirth'])
	and $_POST['username']!='')
  
  
  //clean up data placed in form & not for selected inputs
	  if(get_magic_quotes_gpc())
	  {
		   $_POST['username'] = stripslashes($_POST['username']);
		  $_POST['password'] = stripslashes($_POST['password']);	
		  $_POST['passverify'] = stripslashes($_POST['passverify']);
		  $_POST['emailAddress'] = stripslashes($_POST['emailAddress']);
		  $_POST['mobileNumber'] = stripslashes($_POST['mobileNumber']);
		  $_POST['firstName'] = stripslashes($_POST['firstName']);
		  $_POST['lastName'] = stripslashes($_POST['lastName']);
		  $_POST['country'] = stripslashes($_POST['country']);
				  $_POST['city'] = stripslashes($_POST['city']);
				  $_POST['postCode'] = stripslashes($_POST['postCode']);
				 
				  $_POST['dateOfBirth'] = stripslashes($_POST['dateOfBirth']);
				 
				 
	  }
	  
	  //check if the two passwords are identical
	  if($_POST['password']==$_POST['passverify'])
	  //We check if the password has 6 or more characters
		  if(strlen($_POST['password'])>=6)
		  {
			  //We check if the email form is valid
			  if(preg_match('#^(([a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+\.?)*[a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+)@(([a-z0-9-_]+\.?)*[a-z0-9-_]+)\.[a-z]{2,}$#i',$_POST['emailAddress']))
			  {
				  
				  
				  
				 
				  // protect the variables
				  $username = mysql_real_escape_string($_POST['username']);
				  $password = mysql_real_escape_string($_POST['password']);
				  $emailAddress = mysql_real_escape_string($_POST['emailAddress']);
				  
								  $firstName = mysql_real_escape_string($_POST['firstName']);
								  $lastName = mysql_real_escape_string($_POST['lastName']);
								  $country = mysql_real_escape_string($_POST['country']);
								  $city = mysql_real_escape_string($_POST['city']);
								  $relationshipStatus = mysql_real_escape_string($_POST['relationshipStatus']);
								  $postCode = mysql_real_escape_string($_POST['postCode']);
								  $mobileNumber = mysql_real_escape_string($_POST['mobileNumber']);
								  $dateOfBirth = mysql_real_escape_string($_POST['dateOfBirth']);
								  $gender = mysql_real_escape_string($_POST['gender']);
								  
								  //set every user to 0
								  $_POST['accountType'] = 0;
								  $accountType = mysql_real_escape_string($_POST['accountType']);
								 
								
				  //check if there is no other user using the same username
				  $dn = mysql_num_rows(mysql_query('select userID from User where username="'.$username.'"'));
				  if($dn==0)
				  {
					  //count the number of users to give an ID to this one
					  $dn2 = mysql_num_rows(mysql_query('select userID from User'));
					  $id = $dn2+1;
					  //save the informations to the databse
					  if(mysql_query('insert into Member(firstName, lastName, country, city, relationshipStatus, postCode,  
					  mobileNumber, dateOfBirth, gender, emailAddress) values ('.$firstName.', "'.$lastName.'", "'.$country.'", "'.$city.'", "'.$relationshipStatus.'", "'.$postCode.'", "'.$mobileNumber.'", "'.$dateOfBirth.'", "'.$gender.'", "'.$emailAddress.'")'))
					  {
												  //Add user data to the user table
												  mysql_query('insert into User (username, password, accountType) values ("'.$username.'", "'.md5($password).'" , "'.$accountType.'")');
						  //dont display the form
						  $form = false;
  ?>
  <div class="message">Account has been created successfully.<br />
  <a href="account.php">Back to Account area.</a></div>
  <?php
					  }
					  else
					  {
						  //Otherwise,  an error occured
						  $form = true;
						  $message = 'An error occurred while signing up.';
					  }
				  }
				  else
				  {
					  //Otherwise, username is not available
					  $form = true;
					  $message = 'The username you want to use is not available, please choose another one.';
				  }
			  }
			  else
			  {
				  //Otherwise,  email is not valid
				  $form = true;
				  $message = 'The email you entered is not valid.';
			  }
		  }
		  else
		  {
			  //Otherwise,  password is too short
			  $form = true;
			  $message = 'Your password must contain at least 6 characters.';
		  }
	  
	  else
	  {
		  //Otherwise,  passwords are not identical
		  $form = true;
		  $message = 'The passwords you entered are not identical.';
	  }
  
  
  
  if($form)
  {
	  //display a message if necessary
	  if(isset($message))
	  {
		  echo '<div class="message">'.$message.'</div>';
	  }
	  // display the form
  
  
  ?>
  
		<form action="sign_up.php" method="post">
			Please fill the following form to sign up:<br /><br />
				<label for="username">Username *</label><input type="text" name="username" value="
				<?php if(isset($_POST['username'])){echo htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');} ?>" /><br />
				<label for="password">Password *<span class="small">(6 characters min)</span></label><input type="password" name=               "password" /><br />
				<label for="passverify">Password *<span class="small">(verify)</span></label><input type="password" name=              "passverify" /><br />
				<label for="emailAddress">Email *</label><input type="text" name="emailAddress" value="<?php if(isset($_POST[                'emailAddress'])){echo htmlentities($_POST['emailAddress'], ENT_QUOTES, 'UTF-8');} ?>" /><br />
				
				<label for="firstName">First Name</label><input type="text" name="firstName" /><br />
				<label for="lastName">Last Name</label><input type="text" name="lastName" /><br />
				<label for="relationshipStatus">Relationship Status *</label><select name="relationshipStatus" /><br />
				<option value="Single">Single</option>
				<option value="Taken">Taken</option>            
				</select>    
				<label for="country">Country</label><input type="text" name="country" /><br />
				<label for="city">City</label><input type="text" name="city" /><br />
				<label for="postCode">Postcode *</label><input type="text" name="postCode" /><br />
				<label for="mobileNumber">Mobile number *</label><input type="text" name="mobileNumber" /><br />
				<label for="gender">Gender</label><select name="gender">
				<option value="Male">Male</option>
				<option value="Female">Female</option>            
				</select> 
				<label for="dateOfBirth">Date Of Birth *</label><input type="text" name="dateOfBirth" /> (Format: DD-MM-YYYY)<br />
			   
				<br /><br />
				<label>* - Required fields.</label><br /><br />                                                                                      
				<input type="submit" value="Create Account" /><br />
				
			  
			  
	  </form>
	  
  <?php
  }
  ?>
  
  <?php
  
  ?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with register system

Post by requinix »

That message happens when the mysql_query() fails, and 99% of the time that happens because there's a syntax error in the query. So what you should be doing now is echoing out the query (before you try to run it) and see what's wrong with it. Maybe try running the query somewhere else - phpMyAdmin or MySQL Workbench or someplace - and seeing the error you get. In fact you could skip most of that by seeing what mysql_error says was the problem.

Spoiler: the problem is with the firstName.
Post Reply