Jump to content

[SOLVED] Creating DOB variable to be written to MySQL


xstevey_bx

Recommended Posts

Im creating a regitration form for my website and I have password encryption sorted and I can write simple inputs to the database.

I am however having trouble with the DOB field.

 

<select id="day" name="day" class="signupinput1">
<option value="-1">Day</option>
<option value="1">1</option>
</select>
<select id="month" name="month" class="signupinput1">
<option value="-1">Month</option>
<option value="1">January</option>
</select>
<select id="year" name="year" class="signupinput1">
<option value="2008">2008</option>
</select>

 

This is the html.  What is the best way to construct a variable to write to the database. What data type is best e.g. Date, datetime, timestamp?

 

Thanks for any help

since you don't need the time at all, the DATE type of field is your best bet.  simply make sure you're padding all month and day values (i assume you'll construct a loop to populate those day/month select boxes) and then construct it according to:

 

YYYY-MM-DD

 

i would caution you on ensuring that you don't allow invalid day numbers for the various months (ie. 30 days has september, april, june and november; all others have 31, february has 28/29).  you might be best off plugging/playing a calendar selection system from any one of the public scripts sites out there.

$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];

if ($year != 'none' || $month != 'none' || $day != 'none') {

if ($year == 'none' || $month == 'none' || $day == 'none') {
	$errors[] = 'The birthdate you provided was not complete.';
} else {
	$dob = "$year"."$month"."$day";
}
}

if (empty($errors)) {
$query = "INSERT INTO users (dob)
	VALUES('$dob')";
mysql_query($query) or die(mysql_error());
mysql_close();
} else {
echo "$errors";
exit();
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.