<?php include "../config/koneksi.php"; ?>

<h2>Kelola Galeri</h2>

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="foto" required>
    <input type="text" name="keterangan" placeholder="Keterangan">
    <button type="submit" name="upload">Upload</button>
</form>

<hr>

<div class="row">
<?php
$data = mysqli_query($koneksi, "SELECT * FROM galeri");
while($d = mysqli_fetch_array($data)){
?>
    <div class="col-md-3">
        <div class="card p-2">
            <img src="../uploads/<?= $d['foto'] ?>" width="100%">
            <p><?= $d['keterangan'] ?></p>

            <a href="hapus_galeri.php?id=<?= $d['id'] ?>" class="btn btn-danger btn-sm">Hapus</a>
        </div>
    </div>
<?php } ?>
</div>

<?php
if(isset($_POST['upload'])){
    $nama = $_FILES['foto']['name'];
    $tmp = $_FILES['foto']['tmp_name'];

    move_uploaded_file($tmp, "../uploads/".$nama);

    mysqli_query($koneksi, "INSERT INTO galeri VALUES('', '$nama', '$_POST[keterangan]')");

    echo "<script>location='kelola_galeri.php'</script>";
}
?>