File "deletemandatory.php"

Full Path: /home/u593703731/domains/anandinternationalschool.com/public_html/controller/deletemandatory.php
File size: 1.18 KB
MIME-type: text/x-php
Charset: utf-8

<?php
session_start();
include "../admin/includes/conn.php"; 

if(!isset($_GET['id']) || empty($_GET['id'])){
    $_SESSION['error'] = "Invalid record ID.";
    header("Location: ../admin/index.php?page=mandatory");
    exit;
}

$id = intval($_GET['id']);


$query = "SELECT files FROM mandatorys WHERE id = $id LIMIT 1";
$res = mysqli_query($conn, $query);

if(mysqli_num_rows($res) == 0){
    $_SESSION['error'] = "Record not found.";
    header("Location: ../admin/index.php?page=mandatory");
    exit;
}

$record = mysqli_fetch_assoc($res);
$file = $record['files'];


$delete = mysqli_query($conn, "DELETE FROM mandatorys WHERE id = $id");

if($delete){
   
    $upload_dir = "assets/mandatory/";
    if(!empty($file) && file_exists($upload_dir.$file)){
        unlink($upload_dir.$file);
    }

     if (!empty($media_file) && !filter_var($media_file, FILTER_VALIDATE_URL)) {
        if (file_exists($media_file)) {
            unlink($media_file);
        }
    }
    $_SESSION['success'] = "Record deleted successfully!";
} else {
    $_SESSION['error'] = "Failed to delete record.";
}

header("Location: ../admin/index.php?page=mandatory");
exit;
?>