我正在尝试在注册表单中添加日,月和年的字段,并将其添加到数据库中的用户记录.所以我说这是形式:
<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>
Run Code Online (Sandbox Code Playgroud)
然后在php部分这个
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
));
....
Run Code Online (Sandbox Code Playgroud)
当我点击注册时,所有内容都插入到数据库中,但日期是0000-00-00.我在下拉菜单中选择的内容并不重要.
user_birthday数据库中的字段是DATE.为什么不保存所选内容?
Pup*_*pil 14
您正在尝试插入的数据库格式DATE是 ,YYYY-MM-DDDD-MM-YYYY
这就是为什么它不插入和取默认值:0000-00-00.
更改:
$dateOfBirth = $_POST['day']."-". $_POST['month']."-".$_POST['year'];
Run Code Online (Sandbox Code Playgroud)
至
$dateOfBirth = $_POST['year']."-". $_POST['month']."-".$_POST['day'];
Run Code Online (Sandbox Code Playgroud)
另一种方法:
使用数组 $dateOfBirth
$dobArr = array($_POST['year'], $_POST['month'], $_POST['day']);
$dateOfBirth = implode('-', $dobArr);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14948 次 |
| 最近记录: |