我已经设置了我的数据库并设置了一个带有 DATE
-Type 的字段。当我查看我的数据库时,格式为:YYYY-MM-DD
但我只能在插入此格式时更新此字段:YYYYMMDD
我的声明。
为什么插入格式不是 DATE
-Type 格式?
我的 PDO 声明:
$stmt = $db->prepare("UPDATE mitarbeiter SET abwesend_von = :abwesend_von, abwesend_bis = :abwesend_bis WHERE id = :id");
$stmt->execute(array(
':abwesend_von' => $_POST['abwesend_von'],
':abwesend_bis' => $_POST['abwesend_bis'],
':id' => $_POST['mitarbeiternummer_abwesenheit']
));
我的 POST 数据:
mitarbeiternummer_abwesenheit:26
abwesend_von:20171001
abwesend_bis:20171031
一些信息:
- 数据库:服务器版本:5.6.19-67.0-log
- 数据库:MySQL-Client-版本:5.1.61
- 编程语言是PHP
最佳答案
Date and time values can be represented in several formats, such as quoted strings or as numbers, depending on the exact type of the value and other factors. For example, in contexts where MySQL expects a date, it interprets any of '2015-07-21', '20150721', and 20150721 as a date.
php 库的参数化机制可能会将参数解释为字符串以外的其他内容,从而导致查询
... SET abwesend_von = 2017-01-05 ...
等同于 ... SET abwesend_von = 2011 ...
;而不是 ... SET abwesend_von = '2017-01-05' ...
关于php - SQL - DATE 类型格式为 YYYY-MM-DD,但 UPDATE 格式为 YYYYMMDD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46591492/