我试图用php在MySQL中插入出生日期,但它显示00-00-0000
,
在html中,我有三个月,日和年的下拉列表,在MySQL中,列为name: birthdate
,Type: DATE
。我尝试使用下面的PHP脚本插入,但我不能。
请参阅以下脚本:
if (isset($_POST['first']) && isset($_POST['last']) && isset($_POST['email']) && isset($_POST['password'])) {
if (!empty($_POST['first']) && !empty($_POST['last']) && !empty($_POST['password'])) {
$user = new User();
$user - > id['id'];
$user - > firstname = $_POST['first'];
$user - > lastname = $_POST['last'];
$user - > email = $_POST['email'];
$user - > password = $_POST['password'];
$month = $_POST['month'];
$dt = $_POST['dt'];
$year = $_POST['year'];
$user - > birthdate = $date_value = "$month/$dt/$year";
if ($user - > Create()) {
header("Location:profile.php");
} else {
echo "<a>User already exist!</a>";
}
}
}
答案 0 :(得分:1)
在PHP中,您使用.
来连接字符串,请尝试:
$Date = $year . "/" . $month. "/" . $dt;
$newformat = date('Y-m-d',$Date);
您的代码应为
$user -> birthdate = $newformat;
答案 1 :(得分:0)
尝试下面的生日(
) $month = date('m', strtotime($month));
/* or you can directly covert like this also $month = date('m', strtotime($_POST['month'])); */
$date_value = $year. "-" .$month . "-".$dt;
$user->birthdate = date('Y-m-d',strtotime($date_value));
此处我已将您的日期转换为Y-m-d格式,因为MySQL日期数据类型接受此格式You can find more about strtotime on this link