230 lines
7.2 KiB
PHP
230 lines
7.2 KiB
PHP
<?php
|
|
// product_add.php
|
|
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
session_start();
|
|
|
|
/* =======================
|
|
1) Kategorie aus GET
|
|
======================= */
|
|
$categoryID = 0;
|
|
if (isset($_GET['categoryID']) && ctype_digit($_GET['categoryID'])) {
|
|
$categoryID = (int)$_GET['categoryID'];
|
|
}
|
|
|
|
/* =======================
|
|
2) DB-Verbindung
|
|
======================= */
|
|
$conn = new mysqli("localhost", "FSST", "L9wUNZZ9Qkbt", "FSST", 3306);
|
|
if ($conn->connect_error) {
|
|
die("Datenbankfehler");
|
|
}
|
|
|
|
/* =======================
|
|
3) Kategorien laden
|
|
======================= */
|
|
$categories = [];
|
|
$result = $conn->query("
|
|
SELECT categoryID, name
|
|
FROM categories
|
|
ORDER BY name
|
|
");
|
|
while ($row = $result->fetch_assoc()) {
|
|
$categories[] = $row;
|
|
}
|
|
|
|
/* =======================
|
|
3b) Marken laden
|
|
======================= */
|
|
$brands = [];
|
|
$result = $conn->query("
|
|
SELECT brandID, name
|
|
FROM brands
|
|
ORDER BY name
|
|
");
|
|
while ($row = $result->fetch_assoc()) {
|
|
$brands[] = $row;
|
|
}
|
|
|
|
/* =======================
|
|
4) Attribute zur Kategorie
|
|
======================= */
|
|
$attributes = [];
|
|
if ($categoryID > 0) {
|
|
$stmt = $conn->prepare("
|
|
SELECT a.attributeID, a.name, a.unit, a.dataType
|
|
FROM categoryAttributes ca
|
|
JOIN attributes a ON a.attributeID = ca.attributeID
|
|
WHERE ca.categoryID = ?
|
|
ORDER BY a.name
|
|
");
|
|
$stmt->bind_param("i", $categoryID);
|
|
$stmt->execute();
|
|
$res = $stmt->get_result();
|
|
while ($row = $res->fetch_assoc()) {
|
|
$attributes[] = $row;
|
|
}
|
|
}
|
|
|
|
/* =======================
|
|
5) Produkt speichern
|
|
======================= */
|
|
$saveError = null;
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['saveProduct'])) {
|
|
|
|
$model = trim($_POST['model']);
|
|
$description = $_POST['description'] ?? null;
|
|
$categoryID = (int)$_POST['categoryID'];
|
|
$brandID = (int)($_POST['brandID'] ?? 0);
|
|
|
|
if ($brandID <= 0) {
|
|
$saveError = 'Bitte eine Marke auswählen.';
|
|
} else {
|
|
// --- Produkt anlegen ---
|
|
$stmt = $conn->prepare("
|
|
INSERT INTO products (categoryID, brandID, model, description)
|
|
VALUES (?, ?, ?, ?)
|
|
");
|
|
$stmt->bind_param("iiss", $categoryID, $brandID, $model, $description);
|
|
$stmt->execute();
|
|
|
|
$productID = $stmt->insert_id;
|
|
|
|
// --- Attribute speichern ---
|
|
if (!empty($_POST['attributes'])) {
|
|
|
|
$stmtAttr = $conn->prepare("
|
|
INSERT INTO productAttributes
|
|
(productID, attributeID, valueString, valueNumber, valueBool)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
");
|
|
|
|
foreach ($_POST['attributes'] as $attributeID => $value) {
|
|
|
|
if ($value === '' || $value === null) {
|
|
continue;
|
|
}
|
|
|
|
$valueString = null;
|
|
$valueNumber = null;
|
|
$valueBool = null;
|
|
|
|
if (is_numeric($value)) {
|
|
$valueNumber = $value;
|
|
} elseif ($value === '0' || $value === '1') {
|
|
$valueBool = (int)$value;
|
|
} else {
|
|
$valueString = trim($value);
|
|
}
|
|
|
|
$stmtAttr->bind_param(
|
|
"iisdi",
|
|
$productID,
|
|
$attributeID,
|
|
$valueString,
|
|
$valueNumber,
|
|
$valueBool
|
|
);
|
|
$stmtAttr->execute();
|
|
}
|
|
}
|
|
|
|
header("Location: productAdder.php?categoryID=" . $categoryID);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
include 'header.php';
|
|
?>
|
|
|
|
<link rel="stylesheet" href="assets/css/login.css">
|
|
<link rel="stylesheet" href="assets/css/productAdder.css">
|
|
|
|
<main class="auth">
|
|
<section class="auth__grid">
|
|
|
|
<!-- Kategorie waehlen -->
|
|
<div class="auth__card">
|
|
<header class="auth__header">
|
|
<h2 class="auth__title">Kategorie wählen</h2>
|
|
</header>
|
|
<form method="get" class="auth__form">
|
|
<div class="auth__select__wrap">
|
|
<label class="auth__select__label" for="categoryID">Kategorie</label>
|
|
<select id="categoryID" name="categoryID" class="auth__select" onchange="this.form.submit()" required>
|
|
<option value="">Kategorie wählen</option>
|
|
<?php foreach ($categories as $cat): ?>
|
|
<option value="<?= $cat['categoryID'] ?>"
|
|
<?= $cat['categoryID'] === $categoryID ? 'selected' : '' ?>>
|
|
<?= htmlspecialchars($cat['name']) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<!-- Produkt anlegen -->
|
|
<?php if ($categoryID > 0): ?>
|
|
<div class="auth__card">
|
|
<header class="auth__header">
|
|
<h2 class="auth__title">Produkt hinzufügen</h2>
|
|
</header>
|
|
|
|
<form method="post" class="auth__form">
|
|
<input type="hidden" name="categoryID" value="<?= $categoryID ?>">
|
|
|
|
<?php if ($saveError): ?>
|
|
<p class="auth__error"><?= htmlspecialchars($saveError) ?></p>
|
|
<?php endif; ?>
|
|
|
|
<label for="brandID">Marke</label>
|
|
<select id="brandID" name="brandID" class="auth__select" required>
|
|
<option value="">Marke wählen</option>
|
|
<?php foreach ($brands as $brand): ?>
|
|
<option value="<?= $brand['brandID'] ?>">
|
|
<?= htmlspecialchars($brand['name']) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
|
|
<label for="model">Modell</label>
|
|
<input id="model" type="text" name="model" class="auth__input" required>
|
|
|
|
<label for="description">Beschreibung</label>
|
|
<textarea id="description" name="description" class="auth__input"></textarea>
|
|
|
|
<h3 class="auth__title">Attribute</h3>
|
|
|
|
<?php foreach ($attributes as $attr): ?>
|
|
<label>
|
|
<?= htmlspecialchars($attr['name']) ?>
|
|
<?php if ($attr['unit']): ?>
|
|
(<?= htmlspecialchars($attr['unit']) ?>)
|
|
<?php endif; ?>
|
|
</label>
|
|
|
|
<input
|
|
type="<?= $attr['dataType'] === 'number' ? 'number' : 'text' ?>"
|
|
name="attributes[<?= $attr['attributeID'] ?>]"
|
|
class="auth__input"
|
|
>
|
|
<?php endforeach; ?>
|
|
|
|
<button type="submit" name="saveProduct" class="auth__input">
|
|
Produkt speichern
|
|
</button>
|
|
</form>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
</section>
|
|
</main>
|
|
|
|
<?php
|
|
$conn->close();
|
|
include 'footer.php';
|
|
?>
|