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

if(!empty($_POST['email']) && !empty($_POST['sex'])){
    $sex = $_POST['sex'];
    $dob = $_POST['dob'];
    $country = $_POST['country'];
    $state = $_POST['state'];
    $email = $_POST['email'];
    $dob = $_POST['dob'];
    $gender = $_POST['sex'];
    $religion = $_POST['religion'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $username = ($_POST['username']);
    $weight = ($_POST['weight']);
    $height = ($_POST['height']);
    $name = $_POST['name'];



     //check for email availability
    $sql = "SELECT * FROM student WHERE email =:email AND username !=:username AND email !=''";
    $conn->query($sql);
    $conn->bind(":email", $email);
    $conn->bind(":username", $username);
    if($conn->rowCount() > 0){
        echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
                    <i class='fas fa-ban'></i> Email already exists for another student.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
           </p>";
        return false;
      }else{

        //process  updating  of data into database

        $sql = "UPDATE student SET sex=:sex,  username=:username, country=:country, 
            dob=:dob, religion=:religion, email=:email, phone=:phone, address=:address, state=:state, height =:height, weight =:weight WHERE username =:username";

        $conn->query($sql);
        $conn->bind(":sex", $sex);
        $conn->bind(":username", $username);
        $conn->bind(":country", $country);
        $conn->bind(":dob", $dob);
        $conn->bind(":religion", $religion);
        $conn->bind(":email", $email);
        $conn->bind(":phone", $phone);
        $conn->bind(":address", $address);
        $conn->bind(":state", $state);
         $conn->bind(":height", $height);
          $conn->bind(":weight", $weight);

        $send = $conn->execute();

        if ($send) {
            echo "<p class='alert alert-success alert-dismissible fade show' role='alert'>
                    <i class='fas fa-check-circle'></i> Student $name was updated successfully.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                  <meta http-equiv='refresh' content='3; student'>
           </p>";
        } else {
            echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
                    <i class='fas fa-ban'></i> An error occurred while adding data.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                  <meta http-equiv='refresh' content='4; student'>
           </p>";
        }

      }

}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>";
}



