oazaSrdceWeb / admin_edit / deletelector.php
deletelector.php
Raw
<?php

/**
 * @file deletelector.php
 * @brief Skript sloužící k smazání konkrétního lektora z databáze a souvisejích fotek a videí.
 *
 * Přístup je povolen pouze přihlášeným uživatelům.
 */
include '../db_connection.php'; ///< Připojení k databázi.
$conn->set_charset("utf8mb4");
session_start();


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $id = $_POST['lectorId'];

    // Získá adresu souboru obsahující fotky a videa lektora
    $sql = "SELECT photoMain, videoMain, photo1, photo2, photo3 FROM lectors WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $stmt->bind_result($photoMain, $videoMain, $photo1, $photo2, $photo3);
    $stmt->fetch();
    $stmt->close();

    // Provede smazání souboru
    $files = [$photoMain, $videoMain, $photo1, $photo2, $photo3];
    foreach ($files as $file) {
        if ($file && file_exists('../' . $file)) {
            unlink('../' . $file);
        }
    }

    // Smaže lektora z databáze
    $sql = "DELETE FROM lectors WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $id);
    if ($stmt->execute()) {
        $_SESSION['success'] = 'Lektor byl úspěšně smazán';
    } else {
        $_SESSION['error'] = 'Vyskytla se chyba';
    }
    $stmt->close();
    header('Location: editlector.php');
    exit();
}