首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >预约系统插槽

预约系统插槽
EN

Stack Overflow用户
提问于 2015-04-26 19:29:59
回答 2查看 2.4K关注 0票数 0

我试图建立一个预约系统,在这个系统中,除了预约时间之外,其他一切都可以工作。例如,如果预约时间设定在上午11:30,并且持续一个小时(下午12:30),那么没有人可以在这些时间内预订或预约。我已经将输入的开始和结束时间转换为unix time,以及转换数据库中已经设置的时间,但它正在使我失败。

我试着比较用户设置的两次之间的结束时间,以及数据库中已经设置的两次之间的用户结束时间。我的代码是:

代码语言:javascript
复制
 if ($length == "1 Hour"){
            $edittime = $time;
            $timeedit = strtotime($edittime)+3600;
            $endtime = date('h:i:s', strftime($timeedit));
        } elseif ($length == "1 Hour 30 Minutes") {
            $edittime = $time;
            $timeedit = strtotime($edittime)+5400;
            $endtime = date('h:i:s', strftime($timeedit));
        } elseif ($length == "2 Hour") {
            $edittime = $time;
            $timeedit = strtotime($edittime)+7200;
            $endtime = date('h:i:s', strftime($timeedit));
        } else {
            header("location:Cancel.php");
        }
        /*Comparison of the start and end times, as well as the user input time.*/
        $querysql = "SELECT Time FROM $tablename WHERE Time <= '$endtime' AND Date = '$date'";
        $queryresult = mysqli_query($connection, $querysql);
        /*Validate the query.*/
        if (! $queryresult) {
            echo ("Could not retrieve the sql data : " . mysqli_error($connection) . " " . mysqli_errno($connection));
        }

        /*Array to collect data from the sql query, to compare against the appointment times the user entered.*/
        $count = 0;
        $starttime = $time;
        $secondtime = strtotime($starttime);
        $existapp[$count] = mysqli_fetch_array($queryresult, MYSQLI_NUM);
        while ($existapp[$count] <> "") {
            $temp = $existapp[$count];
            $acquireddata = $temp[$count];
            $appsec = strtotime($acquireddata);
            if ($length == "1 Hour") {
                $existstart = $appsec;
                $existedit = $existstart + 3600;
                $existend = date('h:i:s', strftime($existedit));
            } elseif ($length == "1 Hour 30 Minutes") {
                $existstart = $appsec;
                $existedit = $existstart + 3600;
                $existend = date('h:i:s', strftime($existedit));
            } elseif ($length == "2 Hour") {
                $existstart = $appsec;
                $existedit = $existstart + 3600;
                $existend = date('h:i:s', strftime($existedit));
            }
            /*$timeedit = end time*/
            /*$secondtime = start time*/
            /*$existededit = exisiting appointment*/

            if ($timeedit <= $existedit and $timeedit >= $existstart) {
                 header("location:Cancel.php");
            }
            $count = $count + 1;
        }

如果你需要整份文件,那就大声喊一声。

我一直在不停地搜索,在检查了unix之后,它应该可以工作了!但事实并非如此!):

代码语言:javascript
复制
<?php
    /*Checks if user is logged in, else redirect to home.*/
    session_start();
    if(! $_SESSION['Username']) {
        header("location:Index.php");
    }
    /*Sets variables as the login to the database, as well as tables of interest.*/
    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";
    $tablename = "appointmentinformation";
    $tablenamed = "clientinformation";

    /*Connect to the database server and the database.*/
    $connection = mysqli_connect("$servername", "$username", "$password", "$dbname") or die("Could not connect to the database");
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    /*Retrieve the username from the current session.*/
    $clientusername = $_SESSION['Username'];
    /*Retrieve the ClientID of interest from a table, with a parameter of the username. Limit the amount of results to one row (one result).*/
    $sql = "SELECT ClientID FROM $tablenamed WHERE Username = '$clientusername' LIMIT 1";
    /*Validate the query.*/
    $results = mysqli_query($connection, $sql);
    if (! $results) {
        echo ("Could not select the data : " . mysql_error());
    } else {
        $datarows = mysqli_fetch_row($results);
        $clientid = $datarows[0];
    }
    /*Retrieve user input.*/
    $date = $_POST["date"];
    $time = $_POST["time"];
    $length = $_POST["length"];

    /*Format date*/
    $date = str_replace('/', '-', $date);

    /*Protection from SQL injection attacks.*/
    $date = stripslashes($date);
    $time = stripslashes($time);
    $length = stripslashes($length);
    $date = mysqli_real_escape_string($connection, $date);
    $time = mysqli_real_escape_string($connection, $time);
    $length = mysqli_real_escape_string($connection, $length);

    if ($length == "1 Hour"){
        $edittime = $time;
        $timeedit = strtotime($edittime)+3600;
    } elseif ($length == "1 Hour 30 Minutes") {
        $edittime = $time;
        $timeedit = strtotime($edittime)+5400;
    } elseif ($length == "2 Hour") {
        $edittime = $time;
        $timeedit = strtotime($edittime)+7200;
    } else {
        header("location:Cancel.php");
    }
    /*Comparison of the start and end times, as well as the user input time.*/
    $querysql = "SELECT Time FROM $tablename WHERE Time <= '$endtime' AND Date = '$date'";
    $queryresult = mysqli_query($connection, $querysql);
    /*Validate the query.*/
    if (! $queryresult) {
        echo ("Could not retrieve the sql data : " . mysqli_error($connection) . " " . mysqli_errno($connection));
    }

    /*Array to collect data from the sql query, to compare against the appointment times the user entered.*/
    $count = 0;
    $starttime = $time;
    $secondtime = strtotime($starttime);
    $existapp[$count] = mysqli_fetch_array($queryresult, MYSQLI_NUM);
    while ($existapp[$count] <> "") {
        $temp = $existapp[$count];
        $acquireddata = $temp[$count];
        $appsec = strtotime($acquireddata);
        if ($length == "1 Hour") {
            $existstart = $appsec;
            $existedit = $existstart + 3600;
        } elseif ($length == "1 Hour 30 Minutes") {
            $existstart = $appsec;
            $existedit = $existstart + 3600;
        } elseif ($length == "2 Hour") {
            $existstart = $appsec;
            $existedit = $existstart + 3600;
        }
        /*$timeedit = end time*/
        /*$secondtime = start time*/
        /*$existededit = exisiting appointment*/

        if ($timeedit <= $existedit and $timeedit >= $existstart) {
             header("location:Cancel.php");
        }
        $count = $count + 1;
    }

    /*SELECT query to retrieve data from the database. A complex query due to two parameters.*/
    $sqlquery = "SELECT * FROM $tablename WHERE Date = '$date' AND Time = '$time'";
    $sqlresult = mysqli_query($connection, $sqlquery);
    /*Validate the query.*/
    if (! $sqlresult) {
        echo ("Could not retrieve the sql data : " . mysqli_error($connection) . " " . mysqli_errno($connection));
    }
    $rows = mysqli_fetch_row($sqlresult);
    $date1 = $rows[3];
    $time1 = $rows[4];

    /*Compare the date and times and validate.*/
    if ($date === $date1 && $time = $time1) {
        echo("This date/time is taken!");
        header("location:CreateAppointmentForm.php");
    } else {
        /*Insert the data into the database if validation passes.*/
        $query = "INSERT INTO appointmentinformation (ClientID, Length, Date, Time) VALUES ('$clientid', '$length', '$date', '$time')";
        $result = mysqli_query($connection, $query);
            if ($result) {
                header("Location:UserCP.php");
            } else {
                echo ("Could not insert data : " . mysqli_error($connection) . " " . mysqli_errno($connection));
            } 
    }

?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-26 19:37:47

您应该将strftime改为strtotime。

http://php.net/manual/en/function.strftime.php

http://php.net/manual/en/function.strtotime.php

票数 0
EN

Stack Overflow用户

发布于 2015-04-26 19:37:16

strftime第一个参数是字符串格式,第二个时间戳是http://php.net/manual/en/function.strftime.php,但您将时间戳作为第一个参数:例如,在这里计算的是$timeedit = strtotime($edittime)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29882462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档