<?php
include 'koneksi.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $product_id = $_POST['id'];
    $nama_produk = $_POST['nama'];
    $deskripsi = $_POST['deskripsi'];
    $stok = $_POST['stok'];
    $harga = $_POST['harga'];

    $gambar_tmp = $_FILES['gambar']['tmp_name'];
    $gambarData = file_get_contents($gambar_tmp);

    $sql = "INSERT INTO products (product_id, foto_produk, nama_produk, deskripsi, stok, harga) VALUES (?, ?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ibssii", $product_id, $gambarData, $nama_produk, $deskripsi, $stok, $harga);

    $null = NULL;
    $stmt->send_long_data(1, $gambarData);

    if ($stmt->execute()) {
        header("Location: ../Produk/Produk.php");
        exit();
    } else {
        echo "Gagal tambah data: " . $stmt->error;
    }

    $stmt->close();
    $conn->close();
} else {
    header("Location: ../Produk/produk.php");
    exit();
}
?>
