dongseshu0698 2013-05-29 22:54
浏览 84
已采纳

根据数据库中的值检查重叠时间

I'm making a room booking system where the user enters times to book. The input is all done by clicking boxes, this part is just a check to make sure people don't tamper with the GET values to overwrite things. Here's what I have so far:

$Username = mysql_real_escape_string(($_POST['Username']));
$DateBooked = mysql_real_escape_string(($_POST['DateBooked']));
$Room = mysql_real_escape_string(($_POST['Room']));
$StartTime = mysql_real_escape_string(($_POST['StartTime']));
$EndTime = mysql_real_escape_string(($_POST['EndTime']));

$query="SELECT bookingid,StartTime,EndTime
        FROM bookings
        WHERE DateBooked = '$DateBooked' AND Room='$Room' AND Approved = 1";
$result=mysql_query($query);
$num=mysql_num_rows($result);

$i=1;
while ($i <= $num)
{
    $MinValue=mysql_result($result,$i,"StartTime");
    $MaxValue=mysql_result($result,$i,"EndTime");
    if ((($StartTime >= $MinValue) && ($StartTime <= $maxValue)) ||
            (($EndTime >= $MinValue) && ($EndTime <= $maxValue))) {
        $overflowed=true;
    }
    $i++;
}

if ($overflowed)
{
//Error message
}
else
{
//Save to database
}

My problem is the following:

  • 14:00-16:00 saved in database
  • Attempt to book 13:00-15:00: Error given correctly.
  • Attempt to book 15:00-17:00: No error given.

What am I doing wrong?

Thanks!

  • 写回答

1条回答 默认 最新

  • dslpofp041310584 2013-05-29 23:58
    关注

    You have a few issues:

    First: Your have inconsistent variables $MaxValue and $maxValue.

    Second: Row numbers for mysql_result start at 0.

    Third: Your comparison is incorrect.

    You should change your loop to this:

    $i=0;
    while ($i < $num)
    {
        $MinValue=mysql_result($result,$i,"StartTime");
        $MaxValue=mysql_result($result,$i,"EndTime");
        if (($StartTime < $MaxValue) && ($MinValue < $EndTime)) {
            $overflowed=true;
        }
        $i++;
    }
    

    Edit: You could improve the code in other ways too, for example you could stop the while loop once you've determine there is an overlap. Also, you may not want to loop over every request for a given room on that day. You could have the SQL query return approved requests whose times are in the same window as the new request (allowing you to drop the while loop).

    Edit 2: Here is more information about overlapping dates.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况