Jump to content

Help.. Query problem.


cyrixware

Recommended Posts

Are you using timestamps for start and end times?

 

If so you could do it like this:

 

<?php
$sql   = "SELECT * FROM schedule WHERE Section = '$Section' AND Day = '$Day' AND Room = '$Room' AND ('$startTime' > startTime AND '$startTime' < endTime)";
?>

 

However the fact that you have '$startTime' in quotes suggests that you are using strings...

Link to comment
https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474329
Share on other sites

Source Codes:

 

<?php
  include("../connDB.php");
  
  if($_REQUEST['Submit'] == "Generate Schedule")
  {
    $Subject	  = $_REQUEST['Subj'];
    $Section 	  = $_REQUEST['Section'];
  	$Room	      = $_REQUEST['Room'];
$Day	      = $_REQUEST['Day'];
$startTime    = $_REQUEST['startTime'];
$endTime      = $_REQUEST['endTime'];
if(($endTime < $startTime) || ($endTime == $startTime))
{
?>
<script language="JavaScript">
	alert("Please select another time.");	
	//win = window.open('addSchedule.php','_self');
</script>
<?php
}
else
{
	/*$sql_search = "SELECT * FROM schedule WHERE Section = '$Section' && Day = '$Day' && Room = '$Room' || (startTime = '$startTime')";
	$q_search   = mysql_query($sql_search);
	$q_num	    = mysql_num_rows($q_search);
	if( $q_num != 0 ){
	*/
?>
		<script language="JavaScript">
			//win = window.open('addSchedule.php','_self');
			//alert("Query result. File already exist!");	
		</script>
<?php

	//}
	//else
	//{
		$sql   = "SELECT * FROM schedule WHERE Section = '$Section' AND Day = '$Day' AND Room = '$Room' ";

		$q_add = mysql_query($sql);
		$num   = mysql_num_rows($q_add);
		if( $num != 0 ){
?>
			<script language="JavaScript">
				//win = window.open('addSchedule.php','_self');
				alert("Query result. Time conflict!");	
			</script>
<?php
		}
		else
		{
			$sql = "INSERT INTO schedule (Section, 
										Subject, 
										Room, 
										Day,
										startTime, 
										endTime) VALUES 
									 ('$Section',
									 '$Subject',
									 '$Room',
									 '$Day',
									 '$startTime',
									 '$endTime')";
			if(!$q = mysql_query($sql))
			{
				echo "<font color=#FF0000 size=2 face=Verdana, Arial, Helvetica, sans-serif>
				<center><b>Please select another time.</b></center></font>";
			}		
			elseif(mysql_affected_rows() == 0)
			{
				echo "<font color=#FF0000 size=2 face=Verdana, Arial, Helvetica, sans-serif>
				<center><b>Cannot add record this time...</b></center></font>";
			}
			else
			{
?>
				<script language="JavaScript">
					alert("Record has been succesfully saved! ");
					//win = window.open('addSchedule.php','_self');
				</script>
<?php
			}
		}
	//}
  	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474339
Share on other sites

or use the TIME_TO_SEC mysql function to compare the time difference...

 

mysql.time

 

<?php
$sql   = "SELECT * FROM schedule WHERE Section = '$Section' AND Day = '$Day' AND Room = '$Room' AND startTime > TIME_TO_SEC($startTime) AND endTime < TIME_TO_SEC($startTime) ";
?>

Link to comment
https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474350
Share on other sites

guys stil the same problem. i use this one coz this is correct i think.

 

'$startTime' BETWEEN startTime AND endTime

 

But when i try to include this into the query i can save the time weither the time is conflict or not.

 

$sql  = "SELECT * FROM schedule WHERE Day = '$Day' AND Room = '$Room' AND ('$startTime' BETWEEN startTime AND endTime) ";

 

by the way tnx fooDigi. is there any idea how to fix this one? 

Link to comment
https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474356
Share on other sites

$starttime BETWEEN starttime and endtime isn't the only conflicting condition

[pre]

                              DB          DB

                            starttime    endtime

=========================================================

conflicting bookings            |            |

                        S----------E        |

                                |            |

                                |      S---------E

                                |            |

                          S--------------------E

                                |            |

                                |  S----E    |

                                |            |

=========================================================

OK bookings                    |            |

                      S------E  |            |

                                |            |  S-----E

[/pre]

 

conflicts if (E > starttime) AND (S < endtime)

Link to comment
https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474689
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.