Link to home
Start Free TrialLog in
Avatar of BrighteyesDesign
BrighteyesDesignFlag for Afghanistan

asked on

Add date of birth into database

I am using the code below to submit a registration form but i'm not sure how to add the users date of birth.

I'm guessing it makes sense to join them together and send them to the database rather than using three separate database columns (which I can do ok).

Any ideas how i'd do this?


<?php if (isset($_POST['submitted'])) { // Handle the form.

	require_once (MYSQL);
	
	// Trim all the incoming data:
	$trimmed = array_map('trim', $_POST);
	
	// Assume invalid values:
	$fn = $ln = $e = $p = FALSE;
	
	// Check for a first name:
	if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['first_name'])) {
		$fn = mysqli_real_escape_string ($dbc, $trimmed['first_name']);
	} else {
		echo '<p class="error">Please enter your first name!</p>';
	}
	
	// Check for a last name:
	if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['last_name'])) {
		$ln = mysqli_real_escape_string ($dbc, $trimmed['last_name']);
	} else {
		echo '<p class="error">Please enter your last name!</p>';
	}
	
	// Check for a tel:
	if (preg_match ('/^[0-9 \'.-]{2,40}$/i', $trimmed['tel'])) {
		$te = mysqli_real_escape_string ($dbc, $trimmed['tel']);
	} else {
		echo '<p class="error">Please enter telephone number</p>';
	}
	
	// Check for a fax:
	
		$fa = mysqli_real_escape_string ($dbc, $trimmed['fax']);
	
	
	// Check for company:
	if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['company'])) {
		$co = mysqli_real_escape_string ($dbc, $trimmed['company']);
	} else {
		echo '<p class="error">Please enter your last name!</p>';
	}
	
		// Check for type of business:
	if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['type_business'])) {
		$ty = mysqli_real_escape_string ($dbc, $trimmed['type_business']);
	} else {
		echo '<p class="error">Please enter your business type!</p>';
	}
	
	// Check for an address:
	 {
		$ad = mysqli_real_escape_string ($dbc, $trimmed['address']);
	} 
	
	// Check for member:
	$me = 'member';
	
	// Check for an email address:
	if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {
		$e = mysqli_real_escape_string ($dbc, $trimmed['email']);
	} else {
		echo '<p class="error">Please enter a valid email address!</p>';
	}

	// Check for a password and match against the confirmed password:
	if (preg_match ('/^\w{4,20}$/', $trimmed['password1']) ) {
		if ($trimmed['password1'] == $trimmed['password2']) {
			$p = mysqli_real_escape_string ($dbc, $trimmed['password1']);
		} else {
			echo '<p class="error">Your password did not match the confirmed password!</p>';
		}
	} else {
		echo '<p class="error">Please enter a valid password!</p>';
	}
	
	

	
	if ($fn && $ln && $e && $p) { // If everything's OK...

		// Make sure the email address is available:
		$q = "SELECT user_id FROM users WHERE email='$e'";
		$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
		
		if (mysqli_num_rows($r) == 0) { // Available.
		
			// Create the activation code:
			$a = md5(uniqid(rand(), true));
		
			// Add the user to the database:
			$q = "INSERT INTO users (email, pass, first_name, last_name, active, company, type_business, member, tel, fax, address, registration_date) VALUES ('$e', SHA1('$p'), '$fn', '$ln', '$a', '$co', '$ty', '$me', '$te', '$fa', '$ad', NOW() )";
			$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

			if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
			
				// Send the email:
				$body = "Thank you for registering at <whatever site>. To activate your account, please click on this link:\n\n";
				$body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a";
				mail($trimmed['email'], 'Registration Confirmation', $body, 'From: admin@sitename.com');
				
				// Finish the page:
				echo '<h3>Thank you for registering! </br>A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>';
				
			
				
			} else { // If it did not run OK.
				echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
			}
			
		} else { // The email address is not available.
			echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>';
		}
		
	} else { // If one of the data tests failed.
		echo '<p class="error">Please re-enter your passwords and try again.</p>';
	}

	mysqli_close($dbc);

} // End of the main Submit conditional.
?></td>
                              </tr>
                            </table></td>
                          </tr>
                          <tr>
                            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td><form action="registerworks2.php" method="post">
                                  <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                      <td width="52%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                          <td><b>First Name</b></td>
                                          <td valign="top"><input type="text" name="first_name" size="20" maxlength="20" value="<?php if (isset($trimmed['first_name'])) echo $trimmed['first_name']; ?>" /></td>
                                        </tr>
                                        <tr>
                                          <td><b>Last Name</b></td>
                                          <td><input type="text" name="last_name" size="20" maxlength="40" value="<?php if (isset($trimmed['last_name'])) echo $trimmed['last_name']; ?>" /></td>
                                        </tr>
                                        <tr>
                                          <td>Date of Birth</td>
                                          <td>DD
                                            
                                            <select name="bdayday" id="bdayday">
                                              <option value="01">1</option>
                                              <option value="02">2</option>
                                            </select> 
                                            MM
                                            
                                            <select name="bdaymonth" id="bdaymonth">
                                              <option value="01">1</option>
                                              <option value="02">2</option>
                                            </select> 
                                            YY
                                            <select name="bdayyear" id="bdayyear">
                                              <option value="1920">1920</option>
                                              <option value="1921">1921</option>
                                              
                                            </select></td>
                                        </tr>
                                        <tr>
                                          <td><b>Email Address</b></td>
                                          <td><input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" /></td>
                                        </tr>
                                        <tr>
                                          <td>Telephone</td>
                                          <td><input name="tel" type="text" id="tel" value="<?php if (isset($trimmed['tel'])) echo $trimmed['tel']; ?>" size="30" maxlength="80" /></td>
                                        </tr>
                                        <tr>
                                          <td>Fax</td>
                                          <td><input name="fax" type="text" id="fax" value="<?php if (isset($trimmed['fax'])) echo $trimmed['fax']; ?>" size="30" maxlength="80" /></td>
                                        </tr>
                                        <tr>
                                          <td>Company</td>
                                          <td><input type="text" name="company" id="company" value="<?php if (isset($trimmed['company'])) echo $trimmed['company']; ?>"/></td>
                                        </tr>
                                        <tr>
                                          <td>Type of Business</td>
                                          <td><input type="text" name="type_business" id="type_business" value="<?php if (isset($trimmed['type_business'])) echo $trimmed['type_business']; ?>"/></td>
                                        </tr>
                                        <tr>
                                          <td valign="top">Address</td>
                                          <td><textarea name="address" id="address" value="<?php if (isset($trimmed['address'])) echo $trimmed['address']; ?>"cols="30" rows="5"></textarea></td>
                                        </tr>
                                        </table>
                                        <p>&nbsp;</p>
<p>&nbsp;</p>
                                        <p>&nbsp;</p></td>
                                      <td width="48%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                          <td>&nbsp;</td>
                                          <td>&nbsp;</td>
                                        </tr>
                                        <tr>
                                          <td valign="top">Membership Type</td>
                                          <td><span class="copy">
                                            <label for="Membership">
                                              <input name="member" type="radio" id="radio" value="Guest" checked="checked" />
                                              Guest<br />
  <input type="radio" name="member" id="radio" value="Premium Individual" />
                                              Premium Individual <br />
  <input name="member" type="radio" id="radio" value="Premium Business" />
                                              Premium Business <br />
  <br />
                                            </label>
                                          </span></td>
                                        </tr>
                                        <tr>
                                          <td>&nbsp;</td>
                                          <td>&nbsp;</td>
                                        </tr>
                                        <tr>
                                          <td><b>Password:</b><small>Use only letters, numbers, and the underscore. Must be between 4 and 20 characters long</small></td>
                                          <td><input type="password" name="password1" size="20" maxlength="20" /></td>
                                        </tr>
                                        <tr>
                                          <td><b>Confirm Password</b></td>
                                          <td><input type="password" name="password2" size="20" maxlength="20" /></td>
                                        </tr>
                                      </table></td>
                                    </tr>
                                  </table>
                                  <div align="center">
                                    <input type="submit" name="submit" value="Register" />
                                </div>
                                  <input type="hidden" name="submitted" value="TRUE" />
                                </form>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of haloexpertsexchange
haloexpertsexchange
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This will also depend on what kind of validation that you want for the date pieces before use.
Avatar of BrighteyesDesign

ASKER

Thanks for that Halo, I just get a blank entry in the database though?
if you echo out $birthday what do you get?
My mistake!

That's working perfectly, thanks!
Avatar of mankowitz
Here's one way of validating a date:


if ($bd = mktime(0,0,0,intval($_REQUEST['bdayday'),intval($_REQUEST['bdaymonth'),
intval($_REQUEST['bdayyear') === FALSE)
{
echo '<p class="error">invalid birthdate</p>'
} else {
$bd = date('Ymd', $bd);
}

Open in new window


[...later..]

$q = "INSERT INTO users (email, pass, first_name, last_name, active, company, type_business, member, tel, fax, address, registration_date, bday) VALUES ('$e', SHA1('$p'), '$fn', '$ln', '$a', '$co', '$ty', '$me', '$te', '$fa', '$ad', NOW(), $bd )";

Open in new window