<!--update_school.php-->

<!-- <?php
include('session_validator.php');
include('includes/db.php');

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve form data
    $schoolId = $_POST["editSchoolId"];
    $schoolName = $_POST["editSchoolName"];
    $schoolAddress = $_POST["editSchoolAddress"];
    $establishmentYear = $_POST["editEstablishmentYear"];
    $schoolPhone = $_POST["editSchoolPhone"];
    $schoolEmail = $_POST["editSchoolEmail"];
    $schoolWebsite = $_POST["editSchoolWebsite"];
    $schoolLogo = $_POST["editSchoolLogo"];
    $schoolCode = $_POST["editSchoolCode"];
    $nebCode = $_POST["editNEBCode"];
    $ctevtCode = $_POST["editCTEVTCode"];
    $pan = $_POST["editPAN"];

    // Update the database
    $query = "UPDATE schools SET
        school_name = '$schoolName',
        address = '$schoolAddress',
        establishment_year = '$establishmentYear',
        phone = '$schoolPhone',
        email = '$schoolEmail',
        website = '$schoolWebsite',
        logo = '$schoolLogo',
        school_code = '$schoolCode',
        neb_code = '$nebCode',
        ctevt_code = '$ctevtCode',
        pan = '$pan'
        WHERE id = '$schoolId'";

    $result = $conn->query($query);

    if ($result) {
        echo "School data updated successfully.";
    } else {
        echo "Error updating school data: " . $conn->error;
    }
}

// Close the database connection
$conn = null;
?>
 -->

 <?php
include('includes/db.php');

// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve form data
    $schoolId = $_POST['editSchoolId'];
    $schoolName = $_POST['editSchoolName'];
    $address = $_POST['editSchoolAddress'];
    $establishmentYear = $_POST['editEstablishmentYear'];
    $phone = $_POST['editSchoolPhone'];
    $email = $_POST['editSchoolEmail'];
    $website = $_POST['editSchoolWebsite'];
    $schoolCode = $_POST["editSchoolCode"];
    $nebCode = $_POST["editNEBCode"];
    $ctevtCode = $_POST["editCTEVTCode"];
    $pan = $_POST["editPAN"];

   // Check if a new logo file is uploaded
    if ($_FILES['editSchoolLogo']['error'] == UPLOAD_ERR_OK) {
        $uploadDir = 'uploads/';
        $uploadFile = $uploadDir . basename($_FILES['editSchoolLogo']['name']);

        // Move the uploaded file to the desired directory
        if (move_uploaded_file($_FILES['editSchoolLogo']['tmp_name'], $uploadFile)) {
            // Update the database with the new logo path
            $logoPath = $uploadFile;

            // Perform the database update
            $sql = "UPDATE schools SET
                    school_name = '$schoolName',
                    address = '$address',
                    establishment_year = '$establishmentYear',
                    phone = '$phone',
                    email = '$email',
                    website = '$website',
                    logo = '$logoPath'
                    WHERE id = $schoolId";

            if ($conn->query($sql) === TRUE) {
                // Database update successful
                echo "School data updated successfully.";
            } else {
                // Database update failed
                echo "Error updating school data: " . $conn->error;
            }
        } else {
            // Handle file upload failure
            echo "File upload failed.";
        }
    } else {
        // Update the database without changing the logo
        $sql = "UPDATE schools SET
                school_name = '$schoolName',
                address = '$address',
                establishment_year = '$establishmentYear',
                phone = '$phone',
                email = '$email',
                website = '$website'
                school_code = '$schoolCode',
		        neb_code = '$nebCode',
		        ctevt_code = '$ctevtCode',
		        pan = '$pan'

                WHERE id = $schoolId";

        if ($conn->query($sql) === TRUE) {
            // Database update successful
            echo "School data updated successfully.";
        } else {
            // Database update failed
            echo "Error updating school data: " . $conn->error;
        }
    }
}

// Close the database connection
$conn->close();
?>
