我很难通过循环验证

时间:2014-02-04 02:23:34

标签: php if-statement while-loop

我在我的数据库中:

___________________________
|id|Date      |stime|etime|
|1 |2014-02-02|01:00|02:00|
|2 |2014-02-02|03:00|04:00|

我已手动插入

然后我必须找到一个特定的时间来放置我的其他时间:

  
      
  1. 无法插入相同的stime和etime
  2.   
  3. 无法在stime和etime之间插入
  4.   
  5. 结束时间不能与stime相同
  6.   

我的代码在这里:

      echo  $stime = ($_POST['stime']);
       echo  '----';
       echo  $etime = ($_POST['etime']);
       echo  '<br>';
     $date = "2014-02-02";
  if($stime < $etime){
      $sql_all = "SELECT * FROM time WHERE date='$date' AND (stime='$stime' AND etime='$etime')";
      $query_all = mysql_query($sql_all);  
      $get_rows=  mysql_num_rows($query_all);
      if($get_rows == 1){
        echo 'validate: time already taken choose another time please';
      }else{
        echo 'VALIDATE : insert if the time is not yet taken';
             $new_all = mysql_query("SELECT * FROM time");
            while($get_all = mysql_fetch_assoc($new_all)){
              echo "<br>";
              echo   $g_all_stime = $get_all['stime'];       
              echo "----";
              echo   $g_all_etime = $get_all['etime'];
              echo "<br>";       
                if($stime == $g_all_stime && $etime == $g_all_etime){
                    echo 'There is already a time like yours';  
                }
                elseif($stime <= $g_all_stime && $etime >= $g_all_etime){
                    echo 'cannot insert the same Start and End time';  
                }
                elseif($stime >= $g_all_stime && $etime <= $g_all_etime){
                    echo 'the time must not be between the earlier reserve time';
                }
                elseif($etime >= $g_all_stime && $stime <= $g_all_etime){
                    echo 'the end time must not be the same as the start time and the end time must not 
                    be the same with the end time';
                }
                else{
                    echo 'ELSE no other choice insert here';
                    echo "<br>";
                    echo $sql_insert = "INSERT INTO time(`date`,`stime`,`etime`) VALUES('$date','$stime','$etime')";            
                    //mysql_query($sql_insert);
                }
            }
      }

 }else{
     echo 'do not insert';
 }

然后结果就像这样

01:00----02:00 //this is the input
VALIDATE : insert if the time is not yet taken // validates the input
01:00----02:00 //this part is checking if it has same data type in the database
There is already a time like yours //validates ^
03:00----04:00 //this is the 2nd column in the database
// this always end up in inserting my input
ELSE no other choice insert here
INSERT INTO time(`date`,`stime`,`etime`) VALUES('2014-02-04','01:00','02:00')

请注意我在这里错了

如果已经有这样的问题,你会发布链接

1 个答案:

答案 0 :(得分:2)

首先,您应该使用DATETIME列类型,它将更容易验证。

您可以通过非常简单的查询来完成此操作。

SELECT COUNT(*)
FROM `table`
WHERE 
($start < `end`)
AND
($end > `start`);

如果查询返回任何行,则会抛出错误。

喜欢这个。

<?php
    if ($result > 0) {
        echo 'Error';
    } else {
        echo 'Success';
    }

$ start和$ end变量是您的输入日期。 :)