Solved

Add date of birth into database

Posted on 2011-09-20
6
304 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:BrighteyesDesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
haloexpertsexchange earned 500 total points
ID: 36567508
$bd = mysqli_real_escape_string($dbc, $trimmed['bdayday']);
$bm = mysqli_real_escape_string($dbc, $trimmed['bdaymonth']);
$by = mysqli_real_escape-string($dbc, $trimmed['bdayyear']);
$birthday = $bd."//".$bm."//".$by;<-this will depend on how you want your date formatted

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

Expert Comment

by:haloexpertsexchange
ID: 36567516
This will also depend on what kind of validation that you want for the date pieces before use.
0
 

Author Comment

by:BrighteyesDesign
ID: 36567765
Thanks for that Halo, I just get a blank entry in the database though?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36567796
if you echo out $birthday what do you get?
0
 

Author Comment

by:BrighteyesDesign
ID: 36567806
My mistake!

That's working perfectly, thanks!
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 36567807
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

                 
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question