<?php
session_start();
include "koneksi.php";

if (!isset($_SESSION['id'])) {
    header("Location: login.php");
    exit;
}

$user_id = $_SESSION['id'];

// Ambil data foto
$q = mysqli_query($koneksi, "SELECT foto FROM user WHERE id='$user_id'");
$data = mysqli_fetch_assoc($q);

if ($data && !empty($data['foto'])) {
    $file = "uploads/" . $data['foto'];
    if (file_exists($file)) {
        unlink($file); // hapus file dari folder
    }

    // Hapus nama file dari database
    mysqli_query($koneksi, "UPDATE user SET foto='' WHERE id='$user_id'");
}

header("Location: profil.php");
exit;
