我有一个表格,用户可以在其中查看已批准的休假次数。但是问题开始于用户申请休假时,选择了从月底开始到月初结束的日期。
这是查询
SELECT l.applicationID
, l.empID
, e.name
, l.typeID
, t.title
, t.allowed_leave
, l.start_date
, l.end_date
, l.comment
, l.status
FROM leave_application l
JOIN employee e
ON e.empID = l.empID
JOIN leave_type t
ON t.typeID = l.typeID
where l.empID = $employee_id
AND status = 'approved'
AND start_date LIKE '$start_date%'
现在,当用户尝试选择列表中显示的起始月份但当他选择结束日期时,由于未定义结束日期查询,因此未显示该日期。
答案 0 :(得分:0)
首先你应该避免在你的 SQL 字符串上连接并使用准备好的语句来防止 SQL 注入。然后就可以像这样用SQL日期函数测试年月了;
// getting the input from the request
$inputDate = filter_input(INPUT_POST, 'startDate');
// splitting the string into an array
$dateParts = explode('-', $inputDate);
$sql = 'SELECT .. FROM yourtable WHERE some tests'
// adding the variable part of the query where clause
// the question marks will be replaced by true values
// when you will execute the query
if (count($dateParts) >= 2){
$sql .= ' AND YEAR(startDate)=? AND MONTH(startDate)= ?'
}
if (count($dateParts) == 3){
$sql .= ' AND DAY(startDate)=?'
}
// Preparing and executing the query
$dbConnection = new PDO(...);
$statement = $dbConnection->prepare($sql);
// executing the query passing the values as an array
$statement->execute($dateParts);
希望对大家有帮助