samedi 3 avril 2021

Time validation in php

I wrote this code for a lesson schedule time. Basically it gives the user what lessons are available and what time and date is available so it can register to that class, but if there is a conflict between the hours that the user has chosen an error message should appear. The problem with that code is that it does not compare the hours and it is always adding lessons even though there are hour conflicts, Any suggestions?

<?php
require '../includes/db.php';

session_start();
if(isset($_POST['submit_lecture']))
{

  $user_id=$_SESSION['user_ID'];
  $lectureID=$_POST['lecture'];

  $select_current_lecture = "SELECT * FROM department WHERE id='$lectureID' ";
  $run_current_lecture = mysqli_query($conn, $select_current_lecture);
  $row_lecture=mysqli_fetch_assoc($run_current_lecture);

//New hours that user wants to add to his schedule
  $monday=strtotime($row_lecture['monday']);
  $monday_end=strtotime($row_lecture['monday_end']);
  $tuesday=strtotime($row_lecture['tuesday']);
  $tuesday_end=strtotime($row_lecture['tuesday_end']);
  $wednesday=strtotime($row_lecture['wednesday']);
  $wednesday_end=strtotime($row_lecture['wednesday_end']);
  $thursday=strtotime($row_lecture['thursday']);
  $thursday_end=strtotime($row_lecture['thursday_end']);
  $friday=strtotime($row_lecture['friday']);
  $friday_end=strtotime($row_lecture['friday_end']);


  



  $id_query21 = "SELECT * FROM lectures WHERE student_id='$user_id' AND lecture_id='$lectureID'";
  $id_query_run21 = mysqli_query($conn, $id_query21);

  $id_query2 = "SELECT * FROM lectures WHERE student_id='$user_id' ";
  $id_query_run2 = mysqli_query($conn, $id_query2);


  if(mysqli_num_rows($id_query_run21) > 0 )
  {
    $_SESSION['type']="danger";
    $_SESSION['message_check']="You already chosen this lesson!";
    header('Location: table_lectures.php');

  }else{

    if(mysqli_num_rows($id_query_run2) > 0 )
    { 

      while($rows=mysqli_fetch_assoc($id_query_run2))
      {

//Already Existing hours that user already has signed up

       $lecture_chosen=$rows['lecture_id'];
       $id_query = "SELECT * FROM department WHERE id='$lecture_chosen'";
       $id_query_run = mysqli_query($conn, $id_query);
       $row = mysqli_fetch_assoc($id_query_run);
       $already_monday=strtotime($row['monday']);
       $already_monday_end=strtotime($row['monday_end']);
       $already_tuesday=strtotime($row['tuesday']);
       $already_tuesday_end=strtotime($row['tuesday_end']);
       $already_wednesday=strtotime($row['wednesday']);
       $already_wednesday_end=strtotime($row['wednesday_end']);
       $already_thursday=strtotime($row['thursday']); 
       $already_thursday_end=strtotime($row['thursday_end']);
       $already_friday=strtotime($row['friday']);
       $already_friday_end=strtotime($row['friday_end']);

       

       if((($already_monday < $monday) && ( $monday<$already_monday_end)))
       {
        $_SESSION['type']="danger";
        $_SESSION['message_check']="You cant choose this lesson, hours conflict";
        header('Location: table_lectures.php');
      }
      if((($already_tuesday < $tuesday) && ($tuesday < $already_tuesday_end)))
      {
        $_SESSION['type']="danger";
        $_SESSION['message_check']="You cant choose this lesson, hours conflict";
        header('Location: table_lectures.php');
      }
      if((($already_wednesday<$wednesday) && ($wednesday<$already_wednesday_end)))
      {
        $_SESSION['type']="danger";
        $_SESSION['message_check']="You cant choose this lesson, hours conflict";
        header('Location: table_lectures.php');
      }
      if((($already_thursday<$thursday) && ($thursday<$already_thursday_end)))
      {
        $_SESSION['type']="danger";
        $_SESSION['message_check']="You cant choose this lesson, hours conflict";
        header('Location: table_lectures.php');
      }
      if((($already_friday<$friday) && ($friday<$already_friday_end)))
      {
        $_SESSION['type']="danger";
        $_SESSION['message_check']="You cant choose this lesson, hours conflict";
        header('Location: table_lectures.php');
      }

      

      $query = "INSERT INTO lectures(lecture_id,student_id)
      VALUES('$lectureID','$user_id')";
      
      $query_run2=mysqli_query($conn, $query);


      if($query_run2)
      {
        $_SESSION['type']="success";
        $_SESSION['message_check']="The lesson added successfully";
        header('Location: table_lectures.php');


      }
      else
      {
        $_SESSION['type']="danger";
        $_SESSION['message_check']="Error";
        header('Location: table_lectures.php');

      }
      






    }


  }else{
   $query = "INSERT INTO lectures(lecture_id,student_id)
   VALUES('$lectureID','$user_id')";
   
   $query_run2=mysqli_query($conn, $query);


   if($query_run2)
   {
    $_SESSION['type']="success";
    $_SESSION['message_check']="The lesson added successfully";
    header('Location: table_lectures.php');


  }
  else
  {
    $_SESSION['type']="danger";
    $_SESSION['message_check']="Error";
    header('Location: table_lectures.php');

  }
}





}






} 
?>




Aucun commentaire:

Enregistrer un commentaire