使用MySQLi数据库检查下一个可用日期

时间:2016-01-31 15:05:40

标签: php mysqli calendar

我正在为一个拥有并出租大篷车的人建立一个网站,他想让客户检查可用日期。我一直在使用这个基本脚本来搜索在表单中输入的两个日期之间的任何结果,并在找到任何结果时返回。

以下是当前代码:

<?
             if(isset($_POST['available'])){
                 $start_date = $_POST['start_date'];
                 $end_date = $_POST['end_date'];

$sql = "SELECT * FROM `foiu51r_calendar` WHERE `date` BETWEEN '" . $start_date . "' AND  '" . $end_date . "'";
    $results = $conn->query($sql);

if ($results->num_rows == 0) {
    echo '<div class="alert alert-success" role="alert"><b>Available -</b> The caravan is available for the dates between <b>'.$start_date.'</b> and <b>'.$end_date.'</b>! - <a href="contact.php">Book this holiday!</a></div>';
} else {
    echo '<div class="alert alert-danger" role="alert"><b>Sorry!</b> These dates are not available, please try again or <a href="contact.php">contact us</a>... </div>';
}
             }
             ?>

这里有一些错误,我似乎无法弄清楚:

  1. 大篷车可以在有人离开的同一天搬进去,所以说大篷车是从2016年1月1日 - 8月1日16日预订的,如果我进入日期8/1/16 - 10/1/16它应显示为可用。但我不知道该怎么做。

  2. 显示下一个可用的移动日期将非常方便,但并非完全必要。如果我输入了6/1/16 - 10/1/16,它应该显示如下内容:These dates are not available - next available date is 8/1/16

  3. 如果有人知道如何配置脚本以显示这些结果会有帮助吗?谢谢。

1 个答案:

答案 0 :(得分:0)

1:

<?php
 if(isset($_POST['available'])){
     $start_date = $_POST['start_date'];
     $end_date = $_POST['end_date'];

$sql = "SELECT * FROM `foiu51r_calendar` WHERE `date` BETWEEN '" . $start_date . "' AND  '" . $end_date . "'";
$results = $conn->query($sql);
$counter = mysqli_num_rows($results);

  if ($counter >= 1) {
      echo '<div class="alert alert-success" role="alert"><b>Available -</b> The caravan is available for the dates between <b>'.$start_date.'</b> and <b>'.$end_date.'</b>! - <a href="contact.php">Book this holiday!</a></div>';
  } else {
      echo '<div class="alert alert-danger" role="alert"><b>Sorry!</b> These dates are not available, please try again or <a href="contact.php">contact us</a>... </div>';
  }
}
?>

对于第二个版本,您需要一个在您的数据库中可用的标志,并在大篷车可用的日期将其设为1:

<?php
 if(isset($_POST['available'])){
     $start_date = $_POST['start_date'];
     $end_date = $_POST['end_date'];

$sql = "SELECT * FROM `foiu51r_calendar` WHERE `date` BETWEEN '" . $start_date . "' AND  '" . $end_date . "'";
$results = $conn->query($sql);
$counter = mysqli_num_rows($results);

  if ($counter >= 1) {
      echo '<div class="alert alert-success" role="alert"><b>Available -</b> The caravan is available for the dates between <b>'.$start_date.'</b> and <b>'.$end_date.'</b>! - <a href="contact.php">Book this holiday!</a></div>';
  } else {
      $sql = "SELECT * FROM `foiu51r_calendar` WHERE `available` = 1";
      $result = $conn->query($sql);
      $available_date = $result['date'];
      echo "<div class='alert alert-danger' role='alert'><b>Sorry!</b> These dates are not available. Next available date: '$available_date'";
  }
}
?>