我正在编辑预订系统的代码(wordpress中的国家酒店主题),以便尝试解决当用户在登记日期后的月份选择退房日期时返回0结果的问题。< / p>
以下是预订页面的原始代码:
$dayFirst = date("d",strtotime($_POST["check-in"]));
$monthFirst = date("m",strtotime($_POST["check-in"]));
$yearFirst = date("Y",strtotime($_POST["check-in"]));
$dayLast = date("d",strtotime($_POST["check-out"]));
$monthLast = date("m",strtotime($_POST["check-out"]));
$yearLast = date("Y",strtotime($_POST["check-out"]));
$price = $wpdb->get_row( $wpdb->prepare(
"SELECT min_price FROM {$wpdb->prefix}nation_booking_calendars WHERE id='%d'",
$calID
) );
$minPrice = $price->min_price;
$roomAvailty = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}nation_booking_availability
WHERE calendar_id='%d'
AND day >= $dayFirst
AND month >= $monthFirst
AND year >= $yearFirst
AND day < $dayLast
AND month <= $monthLast
AND year <= $yearLast
AND availability >= %d ",
$calID,$_POST["room-number"]
) );
if ( count($roomAvailty) == count( $dateRange ) ) {
$roomAvailable = true;
}
我的问题是,当预订进入下个月时,count($ roomAvailty)会返回0.
我认为问题是mysql select语句是如何工作的所以我将该部分更改为以下内容:
if($monthFirst==$monthLast) {
$roomAvailty = $wpdb->get_results( $wpdb->prepare(
"SELECT *
FROM {$wpdb->prefix}nation_booking_availability
WHERE calendar_id='%d'
AND day >= $dayFirst
AND month >= $monthFirst
AND year >= $yearFirst
AND day < $dayLast
AND month <= $monthLast
AND year <= $yearLast
AND availability >= %d ",
$calID,$_POST["room-number"]
) );
}
else {
$roomAvailty = $wpdb->get_results( $wpdb->prepare(
"(SELECT * FROM {$wpdb->prefix}nation_booking_availability
WHERE calendar_id='%d'
AND day >= $dayFirst
AND day <= $maxdays
AND month = $monthFirst
AND year = $yearFirst
AND availability >= %d)
UNION ALL
(SELECT * FROM {$wpdb->prefix}nation_booking_availability
WHERE calendar_id='%d'
AND day >= $mindays
AND day < $dayLast
AND month = $monthLast
AND year = $yearLast
AND availability >= %d)
UNION ALL
(SELECT * FROM {$wpdb->prefix}nation_booking_availability
WHERE calendar_id='%d'
AND month > $monthFirst
AND month < $monthLast
AND year = $yearFirst
AND availability >= %d) ",
$calID,$_POST["room-number"]
) );
}
但它仍然返回0结果。有任何想法吗?这是我的数据库的图片:
id calendar_id day month year availability price
101 11 2 9 2014 1 95
102 11 3 9 2014 1 95
103 11 4 9 2014 1 95
104 11 5 9 2014 1 95
105 11 6 9 2014 1 95
106 11 7 9 2014 1 95
107 11 8 9 2014 1 95
108 11 9 9 2014 1 95
109 11 10 9 2014 1 95