我用的是Mysql数据库现在数据库里面有两个字段 分别是formTime(开始时间)和toTime(结束时间)
PHP中的参数用的是time1(开始时间) 和time2(结束时间)
现在想判断插入的时间不在数据库的开始时间和结束时间范围内
如果时间正确则插入时间   如果错误将弹出对话框说时间错误(如果有更好的也可以)
我能想到的代码如下$sql1 ="SELECT fromTime,toTime  FROM operation WHERE date='$date' and username='$_POST[name]'";
mysql_query($sql1);
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){

$fromTime=$row[fromTime];
$toTime=$row[toTime];
if(strtotime($time1)>strtotime($fromTime)&&strtotime($time2)<strtotime($toTime) ){ }elseif(strtotime($time1)>strtotime($formTime)&&strtotime($time2)>strtotime($toTime1)){ }elseif(strtotime($time1)<strtotime($formTime1)&&strtotime($time2)<strtotime($toTime1)){ }elseif(strtotime($time1)<strtotime($formTime1)&&strtotime($time2)>strtotime($toTime1)){ }else{

$sql="insert into operation(username,date,fromTime ,toTime,content,progress) values('$_POST[name]','$date','$time1','$time2','$_POST[content]','$_POST[progress]')";
mysql_query($sql);
}

解决方案 »

  1.   


    $sql ="SELECT count(*) exists  FROM operation WHERE ((fromTime < '{$time1}' and toTime>'{$time1}') or (fromTime < '{$time2}' and toTime>'{$time2}')) and date='$date' and username='$_POST[name]'";
        mysql_query($sql1);
        $result=mysql_query($sql);
        if($row=mysql_fetch_assoc($result) && $row['exists']){
            //存在时间交叉的数据
        }
      

  2.   

    我没看懂 还有 count(*) exists   是什么意思啊
      

  3.   

    count(*) exists 你把 exists 去掉就看懂了。  
      

  4.   


    count(*) AS exists   这叫假名
    if($row=mysql_fetch_assoc($result) && $row['exists']){  在这里用到.