update_article.php aktualisiert

This commit is contained in:
M_Viper 2024-02-27 20:57:26 +00:00
parent c6033f2ba5
commit 9673b2efdb
1 changed files with 74 additions and 56 deletions

View File

@ -1,56 +1,74 @@
<?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.";
}
?>
<?php
// update_article.php
// Datenbankverbindung herstellen
include_once 'config/config.php';
// Funktion zum Protokollieren von Ereignissen
function logEvent($action, $item_name, $manufacturer, $soll, $ist, $mhd, $ean) {
$log_file = 'log.txt';
$timestamp = date('Y-m-d H:i:s');
$log_entry = "$timestamp - Artikel geändert: \"$item_name\", Hersteller: \"$manufacturer\", Soll: \"$soll\", Ist: \"$ist\", MDH: \"$mhd\", EAN: \"$ean\"" . PHP_EOL;
// Überprüfen, ob die Logdatei existiert, andernfalls erstellen
if (!file_exists($log_file)) {
$new_file = fopen($log_file, 'w') or die("Kann die Logdatei nicht erstellen");
fclose($new_file);
}
// Daten in die Logdatei schreiben
file_put_contents($log_file, $log_entry, FILE_APPEND | LOCK_EX);
}
// Ü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()) {
// Artikel erfolgreich aktualisiert, Protokollierung des Ereignisses
logEvent('UPDATE', $name, $manufacturer, $amount_should, $amount_is, $expiration_date, $barcode);
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.";
}
?>