<?php
// update_article.php

// Datenbankverbindung herstellen
include_once 'config/config.php';

// Überprüfen, ob die Anfrage per AJAX erfolgt ist
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    
    // Überprüfen, ob POST-Daten erhalten wurden
    if(isset($_POST['id'])) {
        // Formulardaten abrufen
        $id = $_POST['id'];
        $name = $_POST['name'];
        $manufacturer = $_POST['manufacturer'];
        $location = $_POST['location'];
        $amount_should = $_POST['amount_should'];
        $amount_is = $_POST['amount_is'];
        $expiration_date = $_POST['expiration_date'];
        $barcode = $_POST['barcode'];

        // Verbindung zur Datenbank herstellen
        $conn = new mysqli($host, $username, $password, $database);
        if ($conn->connect_error) {
            die("Verbindung zur Datenbank fehlgeschlagen: " . $conn->connect_error);
        }

        // SQL-Update-Statement vorbereiten
        $sql = "UPDATE materials SET item_name=?, manufacturer=?, location=?, amount_should=?, amount_is=?, expiration_date=?, barcode=? WHERE id=?";
        
        // SQL-Statement vorbereiten und ausführen
        $stmt = $conn->prepare($sql);

        // Binden der Parameter
        $stmt->bind_param("sssssssi", $name, $manufacturer, $location, $amount_should, $amount_is, $expiration_date, $barcode, $id);
        
        // Überprüfen, ob das Statement erfolgreich ausgeführt wurde
        if ($stmt->execute()) {
            echo "Artikel erfolgreich aktualisiert.";
        } else {
            echo "Fehler beim Aktualisieren des Artikels: " . $stmt->error;
        }

        // Statement schließen
        $stmt->close();

        // Datenbankverbindung schließen
        $conn->close();
    } else {
        echo "Keine POST-Daten erhalten.";
    }
} else {
    // Falls die Anfrage nicht per AJAX erfolgt ist, eine Fehlermeldung ausgeben
    echo "Ungültige Anfrage.";
}
?>