<?php
session_start();
require_once 'config/db.php';

if (isset($_GET['id'])) {
    $id = $conn->real_escape_string($_GET['id']);

    // Fetch photo paths to delete them from server
    $res = $conn->query("SELECT photo_1, photo_2, photo_3 FROM upliners WHERE id = '$id'");
    if ($res->num_rows > 0) {
        $row = $res->fetch_assoc();
        for($i=1; $i<=3; $i++) {
            if(!empty($row['photo_'.$i]) && file_exists($row['photo_'.$i])) {
                unlink($row['photo_'.$i]); // Remove file
            }
        }
    }

    $sql = "DELETE FROM upliners WHERE id = '$id'";
    if ($conn->query($sql) === TRUE) {
        $_SESSION['msg'] = "Upliner and associated photos deleted.";
        $_SESSION['msg_type'] = "success";
    } else {
        $_SESSION['msg'] = "Delete failed: " . $conn->error;
        $_SESSION['msg_type'] = "error";
    }
}

header("Location: manage-upliners.php");
exit();