我的时间段没有预订

时间:2014-04-14 10:32:56

标签: php sql mysqli

我想创建一个基本的时段预订系统。但是我遇到了一些问题。

我创建了一个日历,在那里我可以看到某个日期正在发生的事情:

calendar

它从我的数据库表中获取信息。我正在使用PHPMyAdmin,我在那里手动添加了日期。

Database

正如您在表格中所看到的,我还想使用开始时间和结束时间。

如果我点击日历中的日期,我会创建这样的添加事件链接:

<?php
if (isset ( $_GET ['v'] )) {
    echo "<a href='test.php . ?month=" . $month . "&day=" . $day . "&year=" . $year . "&v=true&f=true'>Add Event</a>";
    if (isset ( $_GET ['f'] )) {
        include ("test.php");
    }
    $sqlEvent = "SELECT * FROM calendar WHERE eventDate='" . $month . "/" . $day . "/" . $year . "'";
    $resultEvents = mysqli_query ( $mysqli1, $sqlEvent );
    echo "<br>";
    while ( $events = mysqli_fetch_array ( $resultEvents ) ) {      
    }
}
?>

点击此按钮会使我的网址看起来像这样:test.php%20.%20?month=04&day=18&year=2014&v=true&f=true#

我已经包含了我的整个test.php文件:

<?php 
$hostname = 'localhost';
$username = 'root';
$password = '';
$dbname = "calendar";

$error = 'Cannot connect to the database';

$mysqli1 = new mysqli ( $hostname, $username, $password, $dbname ) or die ( $error );

if (isset ( $_GET ['day'] )) {
    $day = $_GET ['day'];
} else {
    $day = date ( "j" );
}

if (isset ( $_GET ['month'] )) {
    $month = $_GET ['month'];
} else {
    $month = date ( "n" );
}

if (isset ( $_GET ['year'] )) {
    $year = $_GET ['year'];
} else {
    $year = date ( "Y" );
}

$dateToCompare = $month . '/' . $day . '/' . $year;

    echo "<br/><br/><h3>Reservations</h3>";
    $timearray =  array(8,9,10,11,12,13,14,15,16,17,18,19,20,21,22);
    $tablecolor = 1;
    echo "<table style='width: 90%;'>";
    echo "<tr>";
        echo "<th>";
            echo "Time";
        echo "</th>";
        echo "<th>";
            echo "Status";
        echo "</th>";

    echo "</tr>";
    foreach ($timearray as $timearrays) {
        if($tablecolor %2 == 0) {
            echo "<tr>";
        }
        else {
            echo "<tr style='background-color: rgb(0,100,255); background: rgb(0,100,255);'>";
        }

            echo "<th>";
            if ($timearrays == 8) {echo "<h3>8-9am</h3>";}
            if ($timearrays == 9) {echo "<h3>9-10am</h3>";}
            if ($timearrays == 10) {echo "<h3>10-11am</h3>";}
            if ($timearrays == 11) {echo "<h3>11-12am</h3>";}
            if ($timearrays == 12) {echo "<h3>12-13am</h3>";}
            if ($timearrays == 13) {echo "<h3>13-14am</h3>";}
            //Develop your timeslots here to display as required
            echo "</th>";
            echo "<td>";

            $sql = "SELECT * FROM calendar WHERE eventDate='" . $dateToCompare . "'AND timestart >= $timearrays AND endtime <= $timearrays;";

            $result = mysqli_query($mysqli1,$sql);

            if (mysqli_num_rows($result) == 0) {
                echo "<a href='#'><h3 style='color: rgb(255,0,0);'>Reserve</h3></a>";
            } else {
                echo "<h3>Not Available, taken by someone</h3>";
                while($row = mysql_fetch_array($result)) {
                    echo "<br />";
                }
            }
            echo "</td>";
        echo "</tr>";
        $tablecolor++;
    }
    echo "</table>";

?>

如您所见,我尝试显示我的时段:$sql = "SELECT * FROM calendar WHERE eventDate='" . $dateToCompare . "'AND timestart >= $timearrays AND endtime <= $timearrays;";

我认为我从网址上正确了解了我的日期,但我没有看到对某个特定日期的任何预订。

enter image description here

我希望你们中的一些人可以帮助我。如果我需要提供更多代码,请询问。

如果有人对如何做到这一点有更好的了解,请分享。

我不确定我是否应该对时间保持沉默。我基本上只是希望能够点击“预约”,然后反转那个特定的时间段。那么也许开始和结束时间不是必要的?

1 个答案:

答案 0 :(得分:2)

尝试调试您的应用程序。但我想问题可能是您创建的查询中的$timearrays。我不认为(没有测试过)它的工作原理。

我的建议是,在初始化之后打印$sql并查看它返回的内容。然后打开PHPmyadmin(我猜你正在使用,通过数据的外观),看看它返回的是什么。