i have a database at the links.
so the problem it still insert between the time some friends suggested to loop all the data 1st
then validate but im confused here.
**any help would be great **

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
<?php 
mysql_connect("localhost","root","");
mysql_select_db("windsor_reservation");

if(isset($_POST['reserve'])){

       echo  $stime = ($_POST['stime']);
       echo  '----';
       echo  $etime = ($_POST['etime']);
       echo  '<br>';
       $chk = false;
     $date = date("Y-m-d");
    if($stime < $etime){
     $q = mysql_query("SELECT * FROM time WHERE `date`='$date' AND `stime`>='$stime' AND `etime`<='$etime'");
     $count =  mysql_num_rows($q);
          if($count > 0){
        echo 'Time already taken choose another time please';
      }else{
       $new_all = mysql_query("SELECT * FROM time where date='$date'");
            while($get_all = mysql_fetch_assoc($new_all)){
              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';
                    $chk = false;

                }
                elseif($stime <= $g_all_stime && $etime >= $g_all_etime){
                    echo 'cannot insert the same Start and End time with server';
                    $chk = false;

                }
                elseif($stime > $g_all_stime && $etime < $g_all_etime){
                    echo 'the time must not be between the earlier reserve time';
                     $chk = false;
                }
                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';
                    $chk = false;
                }
                else{
                    echo 'ELSE no other choice insert here';
                    $chk = true;
                }
            }
            if($chk == true){
                echo "insert there";
                mysql_query("INSERT INTO time(date,stime,etime)values('$date','$stime','$etime')");
            }else{
                echo "break the statement";
            }

      }

     } else{
     echo 'do not insert';
 }     
 }           
?>
        <form method="post">

            start<input type="time" name="stime">
            end<input type="time" name="etime"><br>
            <input type="submit" name="reserve" value="reserve">
        </form>
    </body>
</html>

Recommended Answers

All 4 Replies

what respose do you get?

it can insert between the time.
that should print an error

could this be a dtetime format issue? I would echo all the values to see how they compare

Solved it

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
<?php 
mysql_connect("localhost","root","");
mysql_select_db("windsor_reservation");

if(isset($_POST['reserve'])){

       echo  $stime = ($_POST['stime']);
       echo  '----';
       echo  $etime = ($_POST['etime']);
       echo  '<br>';
     $date = date("Y-m-d");
      if($stime < $etime){
     $q = mysql_query("SELECT * FROM time WHERE date='$date' AND stime>='$stime' AND etime<='$etime'");
     $arr_query = mysql_fetch_assoc($q);
     $count =  mysql_num_rows($q);
          if($count > 0){
        echo 'Time already taken choose another time please';
      }else{
       $new_all = mysql_query("SELECT * FROM time where date='$date'");
            while($get_all = mysql_fetch_assoc($new_all)){
              echo   $g_all_stime = $get_all['stime'];       
              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';  

                    break;
                }
                elseif($stime <= $g_all_stime && $etime >= $g_all_etime){
                    echo 'cannot insert the same Start and End time with server';
                     break;

                }
                elseif($stime >= $g_all_stime && $etime <= $g_all_etime){
                    echo 'the time must not be between the earlier reserve time';
                     break;

                }
                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';
                    break;
                }
                else{
                    echo 'ELSE no other choice insert here';
                    echo "<br>";
                    echo mysql_query("INSERT INTO time(date,stime,etime)values('$date','$stime','$etime')");
                    break;
                }
            }
      }
      }
      else{
     echo 'do not insert';
 }

 }


?>
        <form method="post">

            start<input type="time" name="stime">
            end<input type="time" name="etime"><br>
            <input type="submit" name="reserve" value="reserve">
        </form>
    </body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.