Jump to content

Recommended Posts

I have just done a registration tutorial but it only has the input fields username, password1,password2 and email.

 

I want to add firstname, last name and some select menus in the registration, I know it should be easy but can't get it to work. Hoping someone can tell me how to get it working.

 

 <?php
  
      if (isset($_POST['submitted'])) {
  
         $errors = array();

              require_once ('mysql_connect.php');
              
                
      if (eregi('^[[:alnum:]\.\'\-]{4,30}$', stripslashes(trim($_POST['username']))) ) {
  
              $user = mysql_real_escape_string($_POST['username']);
  
              $query = "SELECT username FROM user_registration WHERE username = '$user'";
  
              $result = @mysql_query($query);
  
              $num = @mysql_num_rows($result);
  
             
  
              if ($num> 0) {
   
                  $errors[] = '<font color="red">The username you have chosen has already been taken, please try again.</font>';
   
              } else {
  
                  $username = mysql_real_escape_string($_POST['username']);
  
              }
  
          } else {
  
              $errors[] = '<font color="red">Please provide a valid username between 4 and 30 characters.</font>';

          }
          
             
      if (!eregi('^[a-zA-Z]+[a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) )) {
   
              $errors[] = '<font color="red">Please provide a valid email address.</font>';
   
          } else {
  
              $email = mysql_real_escape_string($_POST['email']);
  
          }

                  
      if (!empty($_POST['password1'])) {
  
              if ($_POST['password1'] != $_POST['password2']) {
  
                  $errors[] = '<font color="red">The 2 passwords you have entered do not match.</font>';
  
              } else {
  
                  $password = $_POST['password1'];
  
              }
  
          } else {
  
              $errors[] = '<font color="red">Please provide a password.</font>';
  
          }
          
        
          
          
          if (empty($errors)) {
  
                      $a = md5(uniqid(rand(), true));
                      
                      
                      
                     
  
           $query = "INSERT INTO user_registration (username, email, password, active) VALUES ('$username', '$email', SHA('$password'), '$a')";
   
             
  
              $result = @mysql_query($query);
  
             

              if (mysql_affected_rows() == 1) {
   ?>

Link to comment
https://forums.phpfreaks.com/topic/89596-solved-simple-registration-question/
Share on other sites

sorry, here is the actual form to fill out

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="register">

<fieldset>
<legend>Username</legend>
<input type="text" name="username" class="contact_fields" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" />
</fieldset><br />

<fieldset>
<legend>Password</legend>
<input name="password1" type="password" class="contact_fields" id="password1" />
</fieldset><br />

<fieldset>
<legend>Password(again)</legend>
<input name="password2" type="password" class="contact_fields" id="password2" />
</fieldset><br />

<fieldset>
<legend>Email</legend>
<input type="text" name="email" class="contact_fields" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" />
</fieldset><br />

<fieldset>
<legend>First Name</legend>
<input type="text" name="firstname" class="contact_fields" />
</fieldset><br />

<fieldset>
<legend>Last Name</legend>
<input type="text" name="lastname" class="contact_fields" />
</fieldset><br />

<fieldset>
<legend>City</legend>
<input type="text" name="city" class="contact_fields" />
</fieldset><br />

<fieldset>
<legend>Province</legend>
<input type="text" name="province" class="contact_fields" />
</fieldset><br />

<fieldset>
<legend>Hobbies/Interests</legend>
<select name="hobbies" size="1" class="contact_fields">
<option value="music">Music</option>
<option value="tv">Television</option>
<option value="reading">Reading</option>
<option value="football">Football</option>
<option value="clubbing">Clubbing</option>
<option value="computers">Computers</option>
</select></fieldset><br />

<fieldset>
<legend>Gender</legend>
<select name="gender" size="1" class="contact_fields">
<option value="male">Male</option>
<option value="female">Female</option>
</select></fieldset><br />

<fieldset>
<legend>How did you hear about Magaluf Showdown.com</legend>
<select name="hear_about_us" size="1" class="contact_fields">
<option value="advertisement">Advertisement</option>
<option value="radio">Radio</option>
<option value="flyer">Flyer</option>
<option value="word of mouth">Word of mouth</option>
<option value="internet">Internet</option>
<option value="other">Other</option>
</select></fieldset><br />

<input type="submit" name="submit_btn" value="register" class="submit_btn" />

<input type="hidden" name="submitted" value="TRUE" />


</form>

Copy your IF statement for your Password that you use.  Omit the part where it checks password 1 with password 2, substitute your new name field you added (seperate IF statements for each), store those into variables, add those variables to your INSERT query.  Make sure you modify the DB to allow for the new fields.

again thankyou, I did what you said and it's done the trick but for some reason the details are not going into my database.

My database has the following fields:-

user_id, email, username, password, firstname, lastname, city, province, hobbies, gender, heard_about us, active

 

and now the revised code starting from the first name input.

 // first name field
	  
	  if (!empty($_POST['firstname'])) {
	  
              
              } else {
	   
	       $firstname = $_POST['firstname'];
		   
		   
		   
		   $errors[] = '<font color="red">Please provide a firstname.</font>';
		   
		   }
		   
		   //last name field
		   
		   if (!empty($_POST['lastname'])) {
	  
              
              } else {
	   
	       $lastname = $_POST['lastname'];
		   
		   
		   
		   $errors[] = '<font color="red">Please provide a lastname.</font>';
		   
		   }
		   
		   //City field
		   
		   if (!empty($_POST['city'])) {
	  
              
              } else {
	   
	       $city = $_POST['city'];
		   
		   
		   
		   $errors[] = '<font color="red">Please provide a City.</font>';
		   
		   }
		   
		   //Province field name
		   
		   if (!empty($_POST['province'])) {
	  
              
              } else {
	   
	       $province = $_POST['province'];
		   
		   
		   
		   $errors[] = '<font color="red">Please provide a Province.</font>';
		   
		   }
		   
		   //hobbies/interest input
		   
		   if (!empty($_POST['hobbies'])) {
	  
              
              } else {
	   
	       $hobbies = $_POST['hobbies'];
		   
		   
		   
		   $errors[] = '<font color="red">Please provide a Hobbie.</font>';
		   
		   }
		   
		   // gender input
		   
		   if (!empty($_POST['gender'])) {
	  
              
              } else {
	   
	       $gender = $_POST['gender'];
		   
		   
		   
		   $errors[] = '<font color="red">Please provide your gender.</font>';
		   
		   }
		   
		   // heard about us input
		   
		   if (!empty($_POST['hear_about_us'])) {
	  
              
              } else {
	   
	       $hear_about_us = $_POST['hear_about_us'];
		   
		   
		   
		   $errors[] = '<font color="red">Please provide where you heard about us</font>';
		   
		   }
		   

	                  if (empty($errors)) {
  
                                 $a = md5(uniqid(rand(), true));
				       
				  
				  

  
         $query = "INSERT INTO user_registration (username, email, password, firstname, lastname, city, province, hobbies, gender, heard_about_us, active) VALUES ('$username', '$email', SHA('$password'), '$firstname', '$lastname', '$city', '$province', '$hobbies', '$gender', '$hear_about_us', '$a')";
   
             
  
              $result = @mysql_query($query);
  
             

              if (mysql_affected_rows() == 1) {

 

Now am I missing something really simple here?

Move your assignments up before the ELSE, right now you have it set so if it's empty to set it, which will never happen

 

if (!empty($_POST['firstname']))

 

}

else {

  $firstname = $_POST['firstname'];

 

to

 

 

if (!empty($_POST['firstname'])) {

$firstname = $_POST['firstname'];

 

}

else {

  $errors[] = '<font color="red">Please provide a firstname.</font>';

 

}

 

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.