<?php
  include "include/dbcon.php";
?>
<?php
  global $conn;

  $pannouce = $_POST["annouce"];
  $pannouce = str_replace("'","\'",$pannouce);
  $pannouce = str_replace('"','\"',$pannouce);
  $phfrom = $_POST["hfrom"];
  $phtill = $_POST["htill"];
  $valid = true;
?>
<?php
  if( $phfrom=="" ){
      $valid = false;
      $_SESSION["errmsg"] = "<span style='color: #FF0000'>#ERROR: the date is not set.</span>";
  }

  if( $phtill=="" ){
      $phtill = $phfrom;
  }else{
      if($phfrom<=$phtill){
          
      }else{
          $valid = false;
          $_SESSION["errmsg"] = "<span style='color: #FF0000'>#ERROR: invalid date.</span>";
      }
  }

  if($pannouce==""){
      $valid = false;
      $_SESSION["errmsg"] = "<span style='color: #FF0000'>#ERROR: the annoucement can not empty.</span>";
  }
?>
<?php
  if( $valid ){
      
      $query = "INSERT INTO punchsys_announce VALUES (null, '$phfrom','$phtill','$pannouce')";
      mysqli_query($conn,$query);
//      echo $query;
      
      header("location: main.php?pg=anc");
  }else{
      header("location: main.php?pg=anc");
  }
?>