<?php
// Start the session
session_start();

// Database connection settings
$host = 'localhost';
$db = 'digupdog_FEED';
$user = 'digupdog_FEEDadmin';
$pass = 'Raimundinho1';
$charset = 'utf8mb4';

// Data Source Name (DSN)
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
];

try {
    // Create a new PDO instance
    $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
    throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

// Check if user is logged in and the ID is set
if (!isset($_SESSION['user_id']) || !isset($_GET['id'])) {
    // Redirect to login page or show an error
    header("Location: login.html");
    exit;
}

// Fetch the ID from the GET request
$linkId = $_GET['id'];

// Prepare and execute the delete statement
$deleteStmt = $pdo->prepare('DELETE FROM pinfeeds WHERE id = ?');
$deleteStmt->execute([$linkId]);

// Redirect after successful deletion
header("Location: " . $_SERVER['HTTP_REFERER']);
exit;
?>
