如何使用php搜索CSV文件以防止双重预订

时间:2015-05-08 21:19:59

标签: php

我之前的帖子太模糊了。我做了很多研究,并认为我可以更具体。

/Ready to Save? Store all data into variables
    if (isset($_POST['submit'])) {
        $ID = $_POST['gameID'];
        $GameName = $_POST['gameName'];
        $GameCost = $_POST['gameCost'];
        $Days = $_POST['days'];
        $Total = $_POST['total'];
        $WantedDate = $_POST['reservationStart'];
        $DateTo = $_POST['DateEnd'];
        $StudentName = $_POST['studentName'];
while (!feof($file_handle)) 
    {
    $loansinfo = fgetcsv($file_handle);
// Make sure we only check data for the game we posted. $WantedDate is also a posted variable
        if($loansinfo[0]==$ID) { 
            $referenceDate = $WantedDate;
            $fromDate = "$loansinfo[5]";
            $toDate = "$loansinfo[6]";
// Convert dates to timestamps (strings to integers)
            $referenceTimestamp = strtotime( $referenceDate );
            $fromTimestamp = strtotime( $fromDate );
            $toTimestamp = strtotime( $toDate );
$isBetween = $referenceTimestamp >= $fromTimestamp and $referenceTimestamp <= $toTimestamp;
            if($isBetween == true) {
            //refuse booking    
            echo('<script type="text/javascript">alert("Game Already Booked");</script>');
            exit;
        }
    }
}
otherwise execute save code

问题是,我总是得到'游戏已经预订'。为什么呢?

1 个答案:

答案 0 :(得分:1)

这是您使用删除的代码中未实际使用的任何内容清理的代码:

if(isset($_POST['submit']))
{
    $referenceTimestamp = strtotime($_POST['reservationStart']);
    while(!feof($file_handle)) 
    {
        $loansinfo = fgetcsv($file_handle);
        if($loansinfo[0] == $_POST['gameID'] && $referenceTimestamp >= strtotime($loansinfo[5]) && $referenceTimestamp <= strtotime($loansinfo[6])) 
        { 
            echo('<script type="text/javascript">alert("Game Already Booked");</script>');
            exit;
        }
    }
}

检查两件事: 1. gameID是否匹配。 2.预订开始是否大于或等于$ loansinfo [5]且小于或等于$ loansinfo [6]。

希望清理碎片会有所帮助。