Ok So right now I am using a script called MicroCalendar to setup a client calendar where A client can add an appointment to it but I need to put something into place that makes sure that they cannot overlap appointments..right Now im just saving appointments to txt files with the date of the appointment on them..here is my current code, i have not tried to check the half hour interval yet..

<?php	
		$myFile = "appointment $_POST[day]-$_POST[month]-$_POST[year].txt";
		$fh = fopen($myFile, 'w');
		fclose($fh);
		$fh1 = fopen($myfile, 'a');
		if($_POST[ok])
		{ 
		 $stringData = "$_POST[name], scheduled an appointment at $_POST[hour]:$_POST[minute] on $_POST[day]-$_POST[month]-$_POST[year]";
		 $handle = @fopen($file, 'r');
		 $contents = @fread($handle, filesize($file));
		 $strPos = strpos( $stringdata , $contents);
		 if ($strPos!== false)
		 {
			 fwrite($fh, $stringData);
			 fclose($fh);
			 echo "<br>$_POST[name], scheduled an appointment at $_POST[hour]:$_POST[minute] on $_POST[day]-$_POST[month]-$_POST[year]";  
		 }
	   ?>

Dani AI

Generated

Short summary and practical fixes for the appointment-overlap problem. 's suggestion to switch from flat files to a database is the right direction — a proper schema and constraints make overlap checks reliable and avoid fragile string-parsing logic. A few concrete points address the immediate bugs in the thread and give a more robust pattern going forward.

The immediate bug: the minutes check should reject any value that is not exactly "00" or "30". Use a logical AND and strict comparisons, and always access POST keys with quotes or via filter helpers. For example:

if ($min !== '00' && $min !== '30') {
    // reject and ask for a valid timeslot
}

Better design: store appointment times in proper SQL types (DATETIME or separate DATE and TIME) and enforce uniqueness at the database level. That avoids race conditions where two requests check-then-insert at the same time. Example table layout (simplified):

CREATE TABLE appointments (
  id INT AUTO_INCREMENT PRIMARY KEY,
  start DATETIME NOT NULL,
  end DATETIME NOT NULL,
  customer_name VARCHAR(255) NOT NULL,
  UNIQUE KEY unique_slot (start)
);

Insert using parameterized queries and handle duplicate-key errors rather than relying only on SELECT-before-INSERT. With PDO you can catch the SQLSTATE for duplicate entries (23000) and present a friendly message:

$stmt = $pdo->prepare("INSERT INTO appointments (start, end, customer_name) VALUES (?, ?, ?)");
try {
    $stmt->execute([$start, $end, $name]);
} catch (PDOException $e) {
    if ($e->getCode() === '23000') {
        // slot taken
    } else {
        throw $e;
    }
}

Extras and troubleshooting: enable error reporting while testing, normalize input with DateTime::createFromFormat, consider time zones, and if appointments can span multiple slots store an end time and check overlap with existing.start < new_end AND existing.end > new_start. Use prepared statements (PDO or mysqli) rather than deprecated mysql_* functions. For PDO docs see PDO documentation and for table/index guidance see the MySQL manual on creating tables and keys (https://dev.mysql.com/doc/refman/en/create-table.html).

Recommended Answers

All 3 Replies

If it's on the web and you're already using PHP you might want to look into using a MySQL database instead of a flatfile, they are much easier to work with.

Ok I wrote it like this for the code using sql..im not so great with PHP so maybe you could lend me a hand on getting this fixed..at the moment it doesnt do anything.

<?php    
        $host="localhost"; // Host name

        $username="root"; // Mysql username

        $password="original"; // Mysql password

        $db_name="cutting_edge"; // Database name

        $tbl_name="appointment"; // Table name
        
        // Connect to server and select databse.

        mysql_connect("$host", "$username", "$password")or die("cannot connect");

        mysql_select_db("$db_name")or die("cannot select DB");



        if($_POST[ok])

        {

	  $date = "$_POST[day]-$_POST[month]-$_POST[year]";
          $name = $_POST['name'];
          $time = "$_POST[hour]:$_POST[minute]";
          $time1 = "$_POST[hour]:00";
          $time2 = "$_POST[hour]:30";
          
          // To protect MySQL injection (more detail about MySQL injection)

          $date = stripslashes($date);

          $name = stripslashes($name);

          $date = mysql_real_escape_string($date);

          $name = mysql_real_escape_string($name);
          $time = stripslashes($time);

          $time = mysql_real_escape_string($time);


         
          $sql="SELECT * FROM $tbl_name WHERE date='$date' and time='$time1'";
          $result=mysql_query($sql);
          if( $result == 0 ){
           
             $sql="SELECT * FROM $tbl_name WHERE date='$date' and time='$time2'";
             $result=mysql_query($sql);
             if($result == 0){
               $sql ="INSERT INTO appointment VALUES( '$date', '$time', '$name' )";
               mysql_query($sql);
             }

          }

         }

       ?>

Ok So I got it to Insert the data in the table but im havin a problem I want to check if the user entered something other than 00 or 30 for their appointment time but right now It isnt working it just gets to the if statement which evaluates as true for everything I enter

<?php    
        $host="localhost"; // Host name

        $username="root"; // Mysql username

        $password="original"; // Mysql password

        $db_name="cutting_edge"; // Database name

        $tbl_name="appoint"; // Table name
        
        // Connect to server and select databse.

        mysql_connect("$host", "$username", "$password")or die("cannot connect");

        mysql_select_db("$db_name")or die("cannot select DB");



        if($_POST[ok])

        {

	  $date = "$_POST[month]-$_POST[day]-$_POST[year]";
          $name = $_POST['name'];
          $time = "$_POST[hour]:$_POST[minute]";
          $min = $_POST['minute'];
          
          if(($min != '00') || ($min != '30')){
            echo "Appointments are every 30 minutes please re-choose your time to accommodate this schedule";
            echo $min;
          }
          else{
            // To protect MySQL injection (more detail about MySQL injection)

            $date = stripslashes($date);

            $name = stripslashes($name);

            $date = mysql_real_escape_string($date);

            $name = mysql_real_escape_string($name);
            $time = stripslashes($time);

            $time = mysql_real_escape_string($time);



            $sql="SELECT * FROM $tbl_name WHERE date='$date' and time='$time'";
            $result=mysql_query($sql);
           
            $count=mysql_num_rows($result);
          
            if( $count == 0){
 
              $sql ="INSERT INTO appoint VALUES( '$date', '$time', '$name' )";
              mysql_query($sql);
               
              echo "$name, Scheduled an appointment for $time on $date";
             }
            else{
              echo "An Appointment Is already Scheduled for this time ";
              echo $time;
             }
           }
         }
?>
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.