【问题标题】:how to show a leave info that starts from the end of month and ends with the first of month?如何显示从月底开始到月初结束的休假信息?
【发布时间】:2021-09-26 06:17:13
【问题描述】:

我有一个表单,用户可以在其中查看他的休假已获批准。但问题开始于用户申请休假时选择从该月的结束日期开始并以该月的开始日期结束的日期。

这是查询

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%'

现在,当用户尝试选择列表中显示的起始月份,但当他选择结束日期时,由于未定义结束日期的查询,因此未显示。

【问题讨论】:

    标签: php jquery mysql sql


    【解决方案1】:

    首先你应该避免连接你的 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);
    

    希望对大家有帮助

    【讨论】:

      猜你喜欢
      • 2021-04-05
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 2020-05-25
      • 2018-07-04
      相关资源
      最近更新 更多