Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/cid/system/
Upload File :
Current File : C:/xampp/htdocs/cid/system/delete_submitted_mista_plan.php

<?php
session_start();
@include 'include/config.php';

// Ensure the user is logged in
if (!isset($_SESSION['username_school'])) {
    header('Location: login.php');
    exit();
}

$username = $_SESSION['username_school'];

// Validate the ID parameter
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid request.");
}

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

// First, get the file path to delete the file from the server
$stmt = $conn->prepare("SELECT file_path FROM submitted_mista_plans WHERE id = ? AND username = ?");
$stmt->bind_param("is", $plan_id, $username);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    die("Record not found or you do not have permission to delete this plan.");
}

$row = $result->fetch_assoc();
$file_path = $row['file_path'];
$stmt->close();

// Delete the record from the database
$stmt = $conn->prepare("DELETE FROM submitted_mista_plans WHERE id = ? AND username = ?");
$stmt->bind_param("is", $plan_id, $username);

if ($stmt->execute()) {
    // Delete the file if it exists
    if (!empty($file_path) && file_exists($file_path)) {
        unlink($file_path);
    }
    $stmt->close();
    header("Location: submitted_mista_plans.php?message=deleted");
    exit();
} else {
    die("Error deleting record: " . $stmt->error);
}
?>