Php 将出生日期添加到数据库
我正在尝试在注册表中添加日、月和年的字段,并将其添加到数据库中的用户记录中。所以我把这张表放在这里:Php 将出生日期添加到数据库,php,mysql,Php,Mysql,我正在尝试在注册表中添加日、月和年的字段,并将其添加到数据库中的用户记录中。所以我把这张表放在这里: <div class="form-group"> <div class="form-inline"> <div class="form-group pull-right"> <select name="year" id="year" class="form-control"> <o
<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>
当我点击register时,所有内容都被插入数据库,但日期是0000-00-00
。我在下拉列表中选择什么并不重要
数据库中用户生日的字段是日期。为什么不保存所选内容?日期的数据库格式为
YYYY-MM-DD
,您正在尝试插入DD-MM-YYYY
这就是为什么它不插入并获取默认值:0000-00-00
更改:
$dateOfBirth = $_POST['day']."-". $_POST['month']."-".$_POST['year'];
到
另一种方法:
使用$dateOfBirth的数组
$dobArr = array($_POST['year'], $_POST['month'], $_POST['day']);
$dateOfBirth = implode('-', $dobArr);
我认为时间格式问题你使用了错误的格式请使用这种格式YYYY MM DDAh愚蠢的我。。我怎么会错过这个。。就在我的正前方。。非常感谢。
$dateOfBirth = $_POST['year']."-". $_POST['month']."-".$_POST['day'];
$dobArr = array($_POST['year'], $_POST['month'], $_POST['day']);
$dateOfBirth = implode('-', $dobArr);