I'm trying to add fields for Day, Month and Year in registration form and add it to user record in database. So I've put this is the form:
<div class="form-group">
<div class="form-inline">
<div class="form-group pull-right">
<select name="year" id="year" class="form-control">
<option value="--" selected>Year</option>
<?php
for($i=date('Y'); $i>1899; $i--) {
$birthdayYear = '';
$selected = '';
if ($birthdayYear == $i) $selected = ' selected="selected"';
print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");
}
?>
</select>
</div>
<div class="form-group pull-right">
<select name="month" id="month" onchange="" class="form-control" size="1">
<option value="--" selected>Month</option>
<option value="01">Jan</option>
...
<option value="12">Dec</option>
</select>
</div>
<div class="form-group pull-right">
<select name="day" id="day" onchange="" class="form-control" size="1">
<option value="--" selected>Day</option>
<option value="01">01</option>
...
<option value="31">31</option>
</select>
</div>
</div>
</div>
Then in the php part this
if(!isset($error)){
//hash the password
$hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT);
//create the activasion code
$activasion = md5(uniqid(rand(),true));
$dateOfBirth = $_POST['day']."-". $_POST['month']."-".$_POST['year'];
try {
$stmt = $pdo->prepare('INSERT INTO users (username,password,email,created,active,user_birthday) VALUES (:username, :password, :email, NOW(), :active, :user_birthday)');
$stmt->execute(array(
':username' => $_POST['username'],
':password' => $hashedpassword,
':email' => $_POST['email'],
':active' => $activasion,
':user_birthday' => $dateOfBirth
));
....
When I hit register everything is inserted into database but the date is 0000-00-00
. It doesn't matter what I choose in dropdowns.
The field for user_birthday
in database is DATE
. Why doesn't save what is selected?
How to use the DATE statement. The default way to store a date in a MySQL database is by using DATE. The proper format of a DATE is: YYYY-MM-DD. If you try to enter a date in a format other than the Year-Month-Day format, it might work but it won't be storing the dates as you expect.
Always use ANSI default string literal format for date i.e. YYYY-MM-DD like below. INSERT INTO EMPLOYEE (EMPID, FULLNAME, DESIGNATION, JOINING, SAL, DEPTNAME) VALUES(8976, 'JOHN', 'JOE', 'ANALYST', '1990-12-12', 30000, 'Analytics'); It will insert your data in RDBMS i.e. MySQL, PostgreSQL, SQL Server.
LocalDate (or logically-equivalent classes likes org. joda. time. LocalDate ) is the best way to represent a date-of-birth (DOB) in Java code.
Database format for DATE
is YYYY-MM-DD
and you are trying to insert DD-MM-YYYY
,
That is why its not inserting and taking default value: 0000-00-00
.
Change:
$dateOfBirth = $_POST['day']."-". $_POST['month']."-".$_POST['year'];
To
$dateOfBirth = $_POST['year']."-". $_POST['month']."-".$_POST['day'];
Another approach:
Use array of $dateOfBirth
$dobArr = array($_POST['year'], $_POST['month'], $_POST['day']);
$dateOfBirth = implode('-', $dobArr);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With