<?php
session_start();
include 'includes/database.php';
include 'includes/functions.php';
$conn= new Functions();

if(!empty($_POST['mark_type']) && !empty($_POST['mark_value'])){
    $mark_type = $_POST['mark_type'];
    $mark_value= $_POST['mark_value'];

    //check if details already exists

    $sql = "SELECT * FROM markpercentage WHERE markpercentage_type=:type AND percentage =:value";
    $conn->query($sql);
    $conn->bind(':type', $mark_type);
    $conn->bind(':value', $mark_value);

    $rowCount = $conn->rowCount();

    if($rowCount > 0){

      echo "<script>

              toastr['error']('Mark Distribution Details Already Exists.');

        </script>";
           return false;

    }else{
            //process inserting of data into database

        $sql = "INSERT INTO markpercentage (markpercentage_type, percentage) 
              VALUES(:markpercentage_type, :percentage)";
        $conn->query($sql);
        $conn->bind(":markpercentage_type", $mark_type);
        $conn->bind(":percentage", $mark_value);

        $send = $conn->execute();
        if ($send) {
               echo "<script>

              toastr['success']('Mark Distribution was added successfully.');

        </script> <meta http-equiv='refresh' content='3; markpercentage'>";


        } else {
               echo "<script>

              toastr['error']('An error occurred while adding data.');

        </script> <meta http-equiv='refresh' content='3; markpercentage'>";
        }
   


    }


  

}else{
    echo "<p class='alert alert-danger alert-dismissible fade show text-center' role='alert'>
              <i class='fas fa-ban'></i>
               Fields marked (*) are required.
                  <button type='button' class='close' data-dismiss='alert' aria-label='Close'>
                             <span aria-hidden='true'>×</span>
                  </button>
           </p>";
}



