java 日期保存到mysql 变为0000,Mysql将我的日期列保存为0000-00-00?

I have inserted all records except the date part which inserted incorrectly as 0000-00-00. What is the problem inside my code?

if(isset($_POST["submit"]))

{

$birthDay= $_POST["Birthday_Year"] . "-" . $_POST["Birthday_Month"] . "-" . $_POST["Birthday_day"];

$sql="SELECT * FROM student WHERE stud_id=1";

$result = mysqli_query($db,$sql) or die("Error: ".mysqli_error($db));

$row=mysqli_fetch_array($result,MYSQLI_ASSOC);

if(mysqli_num_rows($result) == 1)

{

$msg = "Sorry...This Studnent ID is already exist...";

}

else

{

$query = mysqli_query($db, "INSERT INTO student (stud_fname, stud_lname, stud_gfname,stud_id,stud_gender,stud_dob,stud_dep,stud_year,stud_section)

VALUES ('$fname', '$lname', '$gfname','$studid', '$gender', '$birthDay','$department', '$year', '$section')");

if($query)

{

$msg = "Thank You! you are now registered.";

}

}

}

解决方案

In all cases date saved as 0000-00-00 means that MySQL got an invalid value (including no value at all). Check your code to make sure that it always has a valid value format for the date.

Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').

Add some validation to your code:

if (!empty($_POST["Birthday_Year"]) && !empty($_POST["Birthday_Month"]) && !empty($_POST["Birthday_day"]))

{

// do stuff

} else

{

// validation error: some of the values are empty

}

PS: Another problem is, that your code has a hole for some SQL injection attack, if you let user input the values on his own, without validation.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值