1

此查询:payment_date BETWEEN "old date" AND "new date"只有在我们在新日期之前写入旧日期时才会成功运行。如果查询如何使其工作payment_date BETWEEN "new date" AND "old date"?因为有时用户先输入新日期,然后才输入旧日期。示例代码:

$sql = "SELECT customer_name, phone_number, address FROM sale 
        WHERE seller_id=? && $search_by  LIKE ? && payment_date BETWEEN ? AND ?";
$q = $conn->prepare($sql);
$result = $q->execute(array($userid, "%".$search_value."%", $oldPaymentDate, $newPaymentDate));
4

1 回答 1

2

始终验证用户输入,同时只比较日期,然后交换它们的顺序不正确,如下所示:

$date1 = new DateTime($newPaymentDate);
$date2 = new DateTime($oldPaymentDate);

if($date1 < $date2) list($newPaymentDate,$oldPaymentDate)=array($oldPaymentDate,$newPaymentDate);
于 2012-04-29T09:58:34.557 回答