Dateien nach "includes" hochladen
This commit is contained in:
parent
8a78fef45a
commit
2675b1e137
|
@ -0,0 +1,117 @@
|
||||||
|
<?php
|
||||||
|
// Einbeziehen der Konfigurationsdatei für die Datenbankverbindung
|
||||||
|
$config = require_once '../config/config.php';
|
||||||
|
|
||||||
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||||
|
try {
|
||||||
|
// Verbindung zur Datenbank herstellen
|
||||||
|
$dsn = "mysql:host={$config['servername']};dbname={$config['database']};charset=utf8mb4";
|
||||||
|
$pdo = new PDO($dsn, $config['username'], $config['password']);
|
||||||
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
|
||||||
|
// Überprüfen Sie, ob eine Datei hochgeladen wurde
|
||||||
|
if (isset($_FILES["excel_file"]) && $_FILES["excel_file"]["error"] == UPLOAD_ERR_OK) {
|
||||||
|
// Excel-Verarbeitungslogik
|
||||||
|
$inputFileName = $_FILES["excel_file"]["tmp_name"];
|
||||||
|
if (!file_exists($inputFileName)) {
|
||||||
|
die("Die hochgeladene Datei existiert nicht.");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
require_once '../vendor/autoload.php'; // Include autoload file for PhpSpreadsheet
|
||||||
|
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
|
||||||
|
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
|
||||||
|
|
||||||
|
// Durchlaufen Sie die Zeilen der Excel-Datei und fügen Sie die Daten in die Datenbank ein
|
||||||
|
foreach ($sheetData as $row) {
|
||||||
|
// Debugging-Ausgabe
|
||||||
|
var_dump($row);
|
||||||
|
|
||||||
|
// Weitere Verarbeitung...
|
||||||
|
$manufacturer = $row['A'];
|
||||||
|
$set_name = $row['B'];
|
||||||
|
$article_number = $row['C'];
|
||||||
|
$ean_barcode = $row['D'];
|
||||||
|
$part1 = $row['E'];
|
||||||
|
$part1_ean = $row['F'];
|
||||||
|
$part2 = $row['G'];
|
||||||
|
$part2_ean = $row['H'];
|
||||||
|
$part3 = $row['I'];
|
||||||
|
$part3_ean = $row['J'];
|
||||||
|
$part4 = $row['K'];
|
||||||
|
$part4_ean = $row['L'];
|
||||||
|
$quantity = isset($row['M']) ? $row['M'] : 0; // Setze quantity auf 0, wenn sie leer ist
|
||||||
|
|
||||||
|
// Zählen Sie die Anzahl der Pakete
|
||||||
|
$packageCount = 0;
|
||||||
|
if (!empty($part1)) $packageCount++;
|
||||||
|
if (!empty($part2)) $packageCount++;
|
||||||
|
if (!empty($part3)) $packageCount++;
|
||||||
|
if (!empty($part4)) $packageCount++;
|
||||||
|
|
||||||
|
// Überprüfen, ob die Artikelnummer bereits vorhanden ist
|
||||||
|
$stmt_check = $pdo->prepare("SELECT article_number FROM articles WHERE article_number = ?");
|
||||||
|
$stmt_check->execute([$article_number]);
|
||||||
|
$existing_row = $stmt_check->fetch();
|
||||||
|
|
||||||
|
if (!$existing_row) {
|
||||||
|
// Führen Sie die Datenbankoperationen durch, um die Daten einzufügen
|
||||||
|
$stmt_insert = $pdo->prepare("INSERT INTO articles (manufacturer, set_name, article_number, ean_barcode, part1, part1_ean, part2, part2_ean, part3, part3_ean, part4, part4_ean, quantity) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||||
|
$stmt_insert->execute([$manufacturer, $set_name, $article_number, $ean_barcode, $part1, $part1_ean, $part2, $part2_ean, $part3, $part3_ean, $part4, $part4_ean, $quantity]);
|
||||||
|
} else {
|
||||||
|
echo "Die Artikelnummer $article_number existiert bereits in der Datenbank.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
// Fehlerbehandlung beim Lesen der Excel-Datei
|
||||||
|
die("Fehler beim Lesen der Excel-Datei: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Verarbeiten Sie die Daten aus dem Formular
|
||||||
|
$manufacturer = $_POST["manufacturer"];
|
||||||
|
$set_name = $_POST["set_name"];
|
||||||
|
$article_number = $_POST["article_number"];
|
||||||
|
$ean_barcode = $_POST["ean_barcode"];
|
||||||
|
$part1 = $_POST["part1"];
|
||||||
|
$part1_ean = $_POST["part1_ean"];
|
||||||
|
$part2 = $_POST["part2"];
|
||||||
|
$part2_ean = $_POST["part2_ean"];
|
||||||
|
$part3 = $_POST["part3"];
|
||||||
|
$part3_ean = $_POST["part3_ean"];
|
||||||
|
$part4 = $_POST["part4"];
|
||||||
|
$part4_ean = $_POST["part4_ean"];
|
||||||
|
$quantity = isset($_POST["quantity"]) ? $_POST["quantity"] : 0; // Setze quantity auf 0, wenn sie leer ist
|
||||||
|
|
||||||
|
// Zählen Sie die Anzahl der Pakete
|
||||||
|
$packageCount = 0;
|
||||||
|
if (!empty($part1)) $packageCount++;
|
||||||
|
if (!empty($part2)) $packageCount++;
|
||||||
|
if (!empty($part3)) $packageCount++;
|
||||||
|
if (!empty($part4)) $packageCount++;
|
||||||
|
|
||||||
|
// Überprüfen, ob die Artikelnummer bereits vorhanden ist
|
||||||
|
$stmt_check = $pdo->prepare("SELECT article_number FROM articles WHERE article_number = ?");
|
||||||
|
$stmt_check->execute([$article_number]);
|
||||||
|
$existing_row = $stmt_check->fetch();
|
||||||
|
|
||||||
|
if (!$existing_row) {
|
||||||
|
// Führen Sie die Datenbankoperationen durch, um die Daten einzufügen
|
||||||
|
$stmt_insert = $pdo->prepare("INSERT INTO articles (manufacturer, set_name, article_number, ean_barcode, part1, part1_ean, part2, part2_ean, part3, part3_ean, part4, part4_ean, quantity) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||||
|
$stmt_insert->execute([$manufacturer, $set_name, $article_number, $ean_barcode, $part1, $part1_ean, $part2, $part2_ean, $part3, $part3_ean, $part4, $part4_ean, $quantity]);
|
||||||
|
} else {
|
||||||
|
echo "Die Artikelnummer $article_number existiert bereits in der Datenbank.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Weiterleiten oder eine Bestätigungsmeldung anzeigen
|
||||||
|
header("Location: ../public/admin.php");
|
||||||
|
exit();
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
// Fehler bei der Datenbankverbindung oder bei der Datenbankoperation
|
||||||
|
die("Database error: " . $e->getMessage());
|
||||||
|
} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
|
||||||
|
// Fehler beim Lesen der Excel-Datei
|
||||||
|
die("Excel error: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
|
@ -0,0 +1,128 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<link rel="icon" type="image/png" href="../img/logo.png">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Artikel hinzufügen</title>
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
|
||||||
|
<link rel="stylesheet" href="../css/article_form.css">
|
||||||
|
<script>
|
||||||
|
function validateForm() {
|
||||||
|
var articleFields = document.querySelectorAll('.article-field');
|
||||||
|
var excelFile = document.getElementById('excel_file').files[0];
|
||||||
|
|
||||||
|
// Überprüfe, ob eine Excel-Datei hochgeladen wurde
|
||||||
|
if (excelFile) {
|
||||||
|
return true; // Erlaube das Hochladen der Excel-Datei ohne weitere Überprüfung
|
||||||
|
}
|
||||||
|
|
||||||
|
// Überprüfe, ob mindestens ein Artikel-Feld ausgefüllt ist
|
||||||
|
for (var i = 0; i < articleFields.length; i++) {
|
||||||
|
if (articleFields[i].value.trim() !== '') {
|
||||||
|
return true; // Erlaube das Absenden des Formulars
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wenn weder eine Excel-Datei hochgeladen wurde noch Artikel-Felder ausgefüllt sind, zeige eine Warnung an
|
||||||
|
alert('Bitte füllen Sie entweder die Felder aus oder laden Sie eine Excel-Datei hoch.');
|
||||||
|
return false; // Verhindere das Absenden des Formulars
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2>Neuen Artikel hinzufügen</h2>
|
||||||
|
<form action="../includes/add_article.php" method="post" enctype="multipart/form-data" onsubmit="return validateForm()">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="manufacturer">Hersteller:</label>
|
||||||
|
<input type="text" class="article-field" id="manufacturer" name="manufacturer">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="set_name">Set Bezeichnung:</label>
|
||||||
|
<input type="text" class="article-field" id="set_name" name="set_name">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="article_number">Artikelnummer:</label>
|
||||||
|
<input type="text" class="article-field" id="article_number" name="article_number">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="ean_barcode">Set EAN:</label>
|
||||||
|
<input type="text" class="article-field" id="ean_barcode" name="ean_barcode">
|
||||||
|
</div>
|
||||||
|
<!-- Separate Box für die restlichen Felder -->
|
||||||
|
<div class="form-group">
|
||||||
|
<h3>Einzelteile</h3>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="part1">Backofen:</label>
|
||||||
|
<input type="text" class="article-field" id="part1" name="part1">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="part1_ean">Backofen EAN:</label>
|
||||||
|
<input type="text" class="article-field" id="part1_ean" name="part1_ean">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="part2">Ceranfeld:</label>
|
||||||
|
<input type="text" class="article-field" id="part2" name="part2">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="part2_ean">Ceranfeld EAN:</label>
|
||||||
|
<input type="text" class="article-field" id="part2_ean" name="part2_ean">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="part3">Auszug:</label>
|
||||||
|
<input type="text" class="article-field" id="part3" name="part3">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="part3_ean">Auszug EAN:</label>
|
||||||
|
<input type="text" class="article-field" id="part3_ean" name="part3_ean">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="part4">Backblech:</label>
|
||||||
|
<input type="text" class="article-field" id="part4" name="part4">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="part4_ean">Backblech EAN:</label>
|
||||||
|
<input type="text" class="article-field" id="part4_ean" name="part4_ean">
|
||||||
|
</div>
|
||||||
|
<!-- Anzeige des gezählten Pakets -->
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="quantity">Pakete:</label>
|
||||||
|
<input type="text" class="article-field" id="quantity" name="quantity" readonly>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- Upload-Button für Excel-Datei -->
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="excel_file">Import Excel-Datei:</label>
|
||||||
|
<input type="file" id="excel_file" name="excel_file" accept=".xls,.xlsx">
|
||||||
|
</div>
|
||||||
|
<div class="button-group">
|
||||||
|
<input type="submit" value="Add Article">
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
// Zähle die Anzahl der Teile und aktualisiere das Feld "Pakete"
|
||||||
|
function countPackages() {
|
||||||
|
var part1 = document.getElementById('part1').value.trim();
|
||||||
|
var part2 = document.getElementById('part2').value.trim();
|
||||||
|
var part3 = document.getElementById('part3').value.trim();
|
||||||
|
var part4 = document.getElementById('part4').value.trim();
|
||||||
|
var quantityField = document.getElementById('quantity');
|
||||||
|
|
||||||
|
var packageCount = 0;
|
||||||
|
if (part1 !== '') packageCount++;
|
||||||
|
if (part2 !== '') packageCount++;
|
||||||
|
if (part3 !== '') packageCount++;
|
||||||
|
if (part4 !== '') packageCount++;
|
||||||
|
|
||||||
|
quantityField.value = packageCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rufe die Funktion zur Paketzählung auf, wenn sich der Wert in einem der Teilfelder ändert
|
||||||
|
document.querySelectorAll('.article-field').forEach(item => {
|
||||||
|
item.addEventListener('change', countPackages);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
$config = include '../config/config.php';
|
||||||
|
$servername = $config['servername'];
|
||||||
|
$username = $config['username'];
|
||||||
|
$password = $config['password'];
|
||||||
|
$database = $config['database'];
|
||||||
|
|
||||||
|
$conn = new mysqli($servername, $username, $password, $database);
|
||||||
|
|
||||||
|
if ($conn->connect_error) {
|
||||||
|
die("Connection failed: " . $conn->connect_error);
|
||||||
|
}
|
||||||
|
?>
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
|
||||||
|
|
||||||
|
if(isset($_POST["submit"])) {
|
||||||
|
$target_dir = "uploads/";
|
||||||
|
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
|
||||||
|
$uploadOk = 1;
|
||||||
|
$fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
|
||||||
|
|
||||||
|
if ($fileType != "xlsx" && $fileType != "xls") {
|
||||||
|
echo "Sorry, only XLSX and XLS files are allowed.";
|
||||||
|
$uploadOk = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($uploadOk == 0) {
|
||||||
|
echo "Sorry, your file was not uploaded.";
|
||||||
|
} else {
|
||||||
|
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
|
||||||
|
$objPHPExcel = PHPExcel_IOFactory::load($target_file);
|
||||||
|
$worksheet = $objPHPExcel->getActiveSheet();
|
||||||
|
|
||||||
|
foreach ($worksheet->getRowIterator() as $row) {
|
||||||
|
$cellIterator = $row->getCellIterator();
|
||||||
|
$cellIterator->setIterateOnlyExistingCells(FALSE);
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
foreach ($cellIterator as $cell) {
|
||||||
|
$data[] = $cell->getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verarbeiten Sie die Daten hier und fügen Sie die Artikel hinzu
|
||||||
|
// Beispiel: Fügen Sie die Artikel zur Datenbank hinzu
|
||||||
|
// $manufacturer = $data[1];
|
||||||
|
// $article_number = $data[2];
|
||||||
|
// ...
|
||||||
|
|
||||||
|
echo "Article added: " . implode(", ", $data) . "<br>";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "Sorry, there was an error uploading your file.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
Loading…
Reference in New Issue