<?php
require_once "koneksi.php";

$id = $_GET['id'];
$data = mysqli_fetch_array(mysqli_query($koneksi,"SELECT * FROM materi WHERE id=$id"));

if(isset($_POST['update'])){

    $judul = $_POST['judul'];
    $deskripsi = $_POST['deskripsi'];

    // upload file baru (opsional)
    if(!empty($_FILES['file']['name'])){
        $file = time().$_FILES['file']['name'];
        move_uploaded_file($_FILES['file']['tmp_name'], "materi_file/".$file);
    } else {
        $file = $data['file'];
    }

    // upload gambar baru (opsional)
    if(!empty($_FILES['gambar']['name'])){
        $gambar = time().$_FILES['gambar']['name'];
        move_uploaded_file($_FILES['gambar']['tmp_name'], "materi_img/".$gambar);
    } else {
        $gambar = $data['gambar'];
    }

    $koneksi->query("
        UPDATE materi SET 
        judul='$judul',
        deskripsi='$deskripsi',
        file='$file',
        gambar='$gambar'
        WHERE id=$id
    ");

    echo "<script>alert('Artikel diupdate');window.location='dashboard_adm_pengembangandiri.php';</script>";
}
?>

<h3>Edit Artikel</h3>

<form method="post" enctype="multipart/form-data">

<input type="text" name="judul" value="<?= $data['judul'] ?>"><br><br>

<textarea name="deskripsi"><?= $data['deskripsi'] ?></textarea><br><br>

<label>Ganti File</label><br>
<input type="file" name="file"><br><br>

<label>Ganti Gambar</label><br>
<input type="file" name="gambar"><br><br>

<button name="update">Update</button>

</form>