查询无法正确保存日期

时间:2013-09-06 08:51:32

标签: php mysql sql

Mysql查询没有像预期的那样保存出生日期,但只是从php注册表中保存为0000-00-00,这可能是什么问题?

<?php
if(isset($_POST['registration']))
{
    require "connection.php";
    $FirstName = strip_tags($_POST['FirstName']);
    $LastName = strip_tags($_POST['LastName']);
    $Phone = $_POST['Phone'];

    $Month = $_POST['month'];
    $Day = $_POST['day'];
    $Year = $_POST['year'];

    $Date = $Year."-".$Month."-".$Day;
    mysql_query("INSERT INTO users (FirstName,LastName,Phone,DOB)
            VALUES ( '" . $FirstName . "','". $LastName . "',
            '" . $Phone . "','" . $Year . "-" . $Month . "-" . $Day . "')")
            or  die("".mysql_error());
    echo "Successful Registration!";
}
?>       

3 个答案:

答案 0 :(得分:1)

尝试将字符串转换为日期,然后执行查询

$date = $Year . "-" . $Month . "-" . $Day;
$dob = date('Y-m-d H:i:s', strtotime($date));
$query="insert into users set FirstName='".$FirstName."',LastName='".$LastName
        ."',Phone='".$Phone."',DOB='".$dob."'";
mysql_query($query)or  die("".mysql_error());

答案 1 :(得分:0)

您需要更改以下代码,

<强>替换

$Month = $_POST['month'];
$Day = $_POST['day'];
$Year = $_POST['year'];

<强>与

$Month = $_POST['Birthday_Month'];
$Day = $_POST['Birthday_day'];
$Year = $_POST['Birthday_Year'];

因为您之前共享的html使用的字段名称为Birthday_Year,依此类推。

答案 2 :(得分:0)

附加此

 $Date= date('Y-m-d',strtotime($Date));

之后
$Date =$Day ."-".$Month."-".$Year; //note some changes

记入How to collect Date of birth from php form and insert into mysql?

开心:)