File "updatemandatory.php"
Full Path: /home/u593703731/domains/anandinternationalschool.com/public_html/controller/updatemandatory.php
File size: 2.63 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_list");
exit;
}
$id = intval($_GET['id']);
$res = mysqli_query($conn, "SELECT * FROM mandatorys WHERE id=$id LIMIT 1");
if (mysqli_num_rows($res) == 0) {
$_SESSION['error'] = "Record not found.";
header("Location: ../admin/index.php?page=mandatory");
exit;
}
$record = mysqli_fetch_assoc($res);
if (isset($_POST['submit'])) {
$section_id = intval($_POST['sections']);
$doc_name = trim($_POST['doc_name']);
$sub_sections = trim($_POST['sub_sections']);
$media_file = trim($_POST['media_file']);
$doc_file = $record['files'];
if (!empty($_FILES['doc_file']['name'])) {
$file_name = $_FILES['doc_file']['name'];
$file_tmp = $_FILES['doc_file']['tmp_name'];
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
$allowed = ['pdf','doc','docx','jpg','jpeg','png','txt'];
if (!in_array($file_ext, $allowed)) {
$_SESSION['error'] = "Invalid file type.";
header("Location: ../admin/index.php?page=mandatory");
exit;
}
$upload_dir = "assets/mandatory/";
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
$new_file_name = time().'_'.uniqid().'.'.$file_ext;
$destination = $upload_dir.$new_file_name;
if (move_uploaded_file($file_tmp, $destination)) {
if (!empty($record['files']) && file_exists($upload_dir.$record['files'])) {
unlink($upload_dir.$record['files']);
}
$doc_file = $new_file_name;
} else {
$_SESSION['error'] = "File upload failed.";
header("Location: ../admin/index.php?page=mandatory");
exit;
}
}
// ✅ UPDATE INCLUDING media_file
$stmt = $conn->prepare("
UPDATE mandatorys
SET sections=?, docs_name=?, files=?, sub_sections=?, media=?
WHERE id=?
");
$stmt->bind_param(
"issssi",
$section_id,
$doc_name,
$doc_file,
$sub_sections,
$media_file,
$id
);
if ($stmt->execute()) {
$_SESSION['success'] = "Mandatory document updated successfully!";
} else {
$_SESSION['error'] = "Database error: " . $stmt->error;
}
$stmt->close();
header("Location: ../admin/index.php?page=mandatory");
exit;
}
?>