将日期变量传递给sql查询

时间:2012-12-03 15:07:30

标签: php mysql variables date

我有以下用户输入表格。我需要将mmddyyyy组合成一个变量,然后将其传递给另一个页面以在sql查询中使用。当日期被硬编码时,我可以让查询工作,所以我知道它有效。我没有看到很多关于这个主题的问题,但我相信很多人都必须这样做。

<?
$date=$_POST['date'];
if(isset($date) and $date=="search"){
$month=mysql_real_escape_string($_POST['Month']);
$day=mysql_real_escape_string($_POST['Day']);
$year=mysql_real_escape_string($_POST['Year']);
$reserveStartTime=mysql_real_escape_string($_POST['Time']);
$reserveDate="$month/$day/$year";
echo "mm/dd/yyyy format :$reserveDate<br>";
$reserveDate="$year-$month-$day";
echo "YYYY-mm-dd format :$reserveDate<br>";
echo "$reserveStartTime";
}
?>



 <label for="Month"><span class="text"><span class="text"><span class="text"><span class="text"><span class="text">Month</span>:</span></span></span></span></label>
    <span class="text">
    <select name="Month"size="1" id="Month">
      <option value="01">Jan</option>
      <option value="02">Feb</option>
      <option value="03">Mar</option>
      <option value="04">Apr</option>
      <option value="05">May</option>
      <option value="06">Jun</option>
      <option value="07">Jul</option>
      <option value="08">Aug</option>
      <option value="09">Sep</option>
      <option value="10">Oct</option>
      <option value="11">Nov</option>
      <option value="12">Dec</option>
      </select>
    <label for="Day">Day:</label>
    <select name="Day" id="Day">
      <option value="01">1</option>
      <option value="02">2</option>
      <option value="03">3</option>
      <option value="04">4</option>
      <option value="05">5</option>
      <option value="06">6</option>
      <option value="07">7</option>
      <option value="08">8</option>
      <option value="09">9</option>
      <option value="10">10</option>
      <option value="11">11</option>
      <option value="12">12</option>
      <option value="13">13</option>
      <option value="14">14</option>
      <option value="15">15</option>
      <option value="16">16</option>
      <option value="17">17</option>
      <option value="18">18</option>
      <option value="19">19</option>
      <option value="20">20</option>
      <option value="21">21</option>
      <option value="22">22</option>
      <option value="23">23</option>
      <option value="24">24</option>
      <option value="25">25</option>
      <option value="26">26</option>
      <option value="27">27</option>
      <option value="28">28</option>
      <option value="29">29</option>
      <option value="3">30</option>
      <option value="31">31</option>
    </option>
       </select>
    <label for="Year">Year:</label>
    <select name="Year" id="Year">
      <option value="2012">2012</option>
      <option value="2013">2013</option>
      <option value="2014">2014</option>
      <option value="2015">2015</option>
      <option value="2016">2016</option>
      <option value="2017">2017</option>
      </select>
     <label for="Time">Time:</label>
    <select name="Time" id="Time">
      <option value="9:00">9:00 am</option>
      <option value="9:30">9:30 am</option>
      <option value="10:00">10:00 am</option>
      <option value="10:30">10:30 am</option>
      <option value="11:00">11:00 am</option>
      <option value="11:30">11:30 am</option>
      <option value="12:00">12:00 pm</option>
      <option value="12:30">12:30 pm</option>
      <option value="13:00">1:00 pm</option>
      <option value="13:30">1:30 pm</option>
      <option value="14:00">2:00 pm</option>
      <option value="14:30">2:30 pm</option>
      <option value="15:00">3:00 pm</option>
      <option value="15:30">3:30 pm</option>
      <option value="16:00">4:00 pm</option>
      <option value="16:30">4:30 pm</option>
      <option value="17:00">5:00 pm</option>
      <option value="17:30">5:30 pm</option>
      <option value="18:00">6:00 pm</option>
      <option value="18:30">6:30 pm</option>
      <option value="19:00">7:00 pm</option>
      <option value="19:30">7:30 pm</option>
      <option value="20:00">8:00 pm</option>
      <option value="20:30">8:30 pm</option>
      <option value="21:00">9:00 pm</option>
   </select>
    </span></p>
         <p>
      <span class="text">
      <input type="hidden" class="text" name="date"value="search">
    <input type="submit" value="Search" />
      </span></p>
  <p>&nbsp;</p>
</form>
</body>
</html>


It then passes the variable to my sql page..
if (isset($_POST['date']) and $_POST['action']=='Search')

        //Get available times for court reservations

    try
    {
    $sql = "SELECT timeslots.courtId, courtName, courtFee, timeslots.slotId, timeslots.reserveStartTime FROM courts INNER JOIN timeslots ON courts.courtId = timeslots.courtId INNER JOIN bookings ON timeslots.slotId != bookings.slotId WHERE bookings.reserveDate = '$reserveDate' AND  timeslots.reserveStartTime = '$reserveStartTime'
    GROUP BY courtName"; 
    $result = $pdo->query($sql);
    }
    catch (PDOException $e)
    {
        $error = 'Error retrieving courts: ' . $e->getMessage();
    include 'error.html.php'; 
        include 'error.html.php';
        exit();
    }
    //getting all time slots available
    while($row = $result->fetch())
    { 
    $courts[] = array('timeslots.courtId' => $row['courtId'],'courtName' => $row['courtName'],'courtFee' => $row['courtFee'], 'timeslots.slotId' => $row['slotId'], 'reserveStartTime' =>$row['reserveStartTime']);
    }
?>

0 个答案:

没有答案