查询失败: 错误: 类型日期的输入语法无效: “quot;”;第 5 行:在 coalesce(''::


Query failed: ERROR: invalid input syntax for type date: "" LINE 5: where att_date between coalesce(''::date,'01-01-1990'::date)

在这里,我的sql查询有一个小问题。但实际上,我不知道该怎么办。我也在下面写了查询,以便你们可以了解如何解决此错误。立即的帮助肯定会不胜感激。

列"att_date"的类型,定义为 postgres 数据库中的"带时区的时间戳"。

谢谢

select (select emp_code ||' - '||first_name ||' '||last_name from person where person_id =  da.person_id) as emp, att_date, timein, timeout, totalhrs,
        totalothrs, isholiday, (select element_name from elements where element_id = da.element_id) as leavetype,daily_attendance_id
        from daily_attendance da
where att_date between coalesce('$prd_st'::date,'01-01-1990'::date) and coalesce('$prd_end'::date,'12-31-4712'::date)
and ad_client_id =  $_SESSION[clientid]
    order by 3;";

直接原因:空字符串''NULL之间的混淆

''NULL不同。

coalesce(NULL, '01-01-1990'::date)01-01-1990. coalesce('', '01-01-1990'::date)是一个错误,并且会发出您收到的错误消息:

regress=# SELECT coalesce('', '01-01-1990'::date);
ERROR:  invalid input syntax for type date: ""
LINE 1: SELECT coalesce('', '01-01-1990'::date);
                        ^

psql 独立运行时。

如果你想要NULL,写NULL,而不是''

根本原因:不良编码实践

阅读有关SQL注入的PHP手册,然后 http://bobby-tables.com/修复代码以使用PHP数据对象(PDO)或pg_query_params参数数组。PHP程序员通常称之为"预准备语句",尽管"参数化查询"更准确。

这不仅可以解决您的问题,还可以消除代码中巨大的安全漏洞。

切勿将字符串插入到 SQL 中。如果有人欺骗您的应用程序(例如,使用直接 POST、修改表单等)发送');DROP TABLE daily_attendance;--您会非常恼火。

提示:如果将 PHP null 值传递给pg_query_params或 PDO 参数绑定方法,它将作为数据库NULL传递。因此,在使用绑定参数("预准备语句")时,无需特殊情况输入 NULL 关键字。