Mysql saved my date column as 0000-00-00?(Mysql 将我的日期列保存为 0000-00-00?)
问题描述
我已插入除错误插入为 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.";
}
}
}
推荐答案
在所有情况下,日期保存为 0000-00-00 意味着 MySQL 获得了无效值(包括根本没有值).检查您的代码以确保它始终具有有效的日期值格式.
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.
来自 MySQL 文档:
无效的 DATE、DATETIME 或 TIMESTAMP 值将转换为相应类型的零"值('0000-00-00' 或 '0000-00-00 00:00:00').
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:另一个问题是,如果你让用户自己输入值,无需验证.
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.
这篇关于Mysql 将我的日期列保存为 0000-00-00?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mysql 将我的日期列保存为 0000-00-00?


基础教程推荐
- oracle区分大小写的原因? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在多列上分布任意行 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01