add brand selection to product addition form and handle brandID in product saving logic
This commit is contained in:
parent
eb088ab7e5
commit
66ee439cf1
129
productAdder.php
129
productAdder.php
@ -35,6 +35,19 @@ while ($row = $result->fetch_assoc()) {
|
|||||||
$categories[] = $row;
|
$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
|
4) Attribute zur Kategorie
|
||||||
======================= */
|
======================= */
|
||||||
@ -58,63 +71,69 @@ if ($categoryID > 0) {
|
|||||||
/* =======================
|
/* =======================
|
||||||
5) Produkt speichern
|
5) Produkt speichern
|
||||||
======================= */
|
======================= */
|
||||||
|
$saveError = null;
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['saveProduct'])) {
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['saveProduct'])) {
|
||||||
|
|
||||||
$model = trim($_POST['model']);
|
$model = trim($_POST['model']);
|
||||||
$description = $_POST['description'] ?? null;
|
$description = $_POST['description'] ?? null;
|
||||||
$categoryID = (int)$_POST['categoryID'];
|
$categoryID = (int)$_POST['categoryID'];
|
||||||
|
$brandID = (int)($_POST['brandID'] ?? 0);
|
||||||
|
|
||||||
// --- Produkt anlegen ---
|
if ($brandID <= 0) {
|
||||||
$stmt = $conn->prepare("
|
$saveError = 'Bitte eine Marke auswählen.';
|
||||||
INSERT INTO products (categoryID, model, description)
|
} else {
|
||||||
VALUES (?, ?, ?)
|
// --- Produkt anlegen ---
|
||||||
");
|
$stmt = $conn->prepare("
|
||||||
$stmt->bind_param("iss", $categoryID, $model, $description);
|
INSERT INTO products (categoryID, brandID, model, description)
|
||||||
$stmt->execute();
|
VALUES (?, ?, ?, ?)
|
||||||
|
|
||||||
$productID = $stmt->insert_id;
|
|
||||||
|
|
||||||
// --- Attribute speichern ---
|
|
||||||
if (!empty($_POST['attributes'])) {
|
|
||||||
|
|
||||||
$stmtAttr = $conn->prepare("
|
|
||||||
INSERT INTO productAttributes
|
|
||||||
(productID, attributeID, valueString, valueNumber, valueBool)
|
|
||||||
VALUES (?, ?, ?, ?, ?)
|
|
||||||
");
|
");
|
||||||
|
$stmt->bind_param("iiss", $categoryID, $brandID, $model, $description);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
foreach ($_POST['attributes'] as $attributeID => $value) {
|
$productID = $stmt->insert_id;
|
||||||
|
|
||||||
if ($value === '' || $value === null) {
|
// --- Attribute speichern ---
|
||||||
continue;
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
$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: product_add.php?categoryID=" . $categoryID);
|
header("Location: productAdder.php?categoryID=" . $categoryID);
|
||||||
exit;
|
exit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
include 'header.php';
|
include 'header.php';
|
||||||
@ -129,13 +148,13 @@ include 'header.php';
|
|||||||
<!-- Kategorie waehlen -->
|
<!-- Kategorie waehlen -->
|
||||||
<div class="auth__card">
|
<div class="auth__card">
|
||||||
<header class="auth__header">
|
<header class="auth__header">
|
||||||
<h2 class="auth__title">Kategorie waehlen</h2>
|
<h2 class="auth__title">Kategorie wählen</h2>
|
||||||
</header>
|
</header>
|
||||||
<form method="get" class="auth__form">
|
<form method="get" class="auth__form">
|
||||||
<div class="auth__select__wrap">
|
<div class="auth__select__wrap">
|
||||||
<label class="auth__select__label" for="categoryID">Kategorie</label>
|
<label class="auth__select__label" for="categoryID">Kategorie</label>
|
||||||
<select id="categoryID" name="categoryID" class="auth__select" onchange="this.form.submit()" required>
|
<select id="categoryID" name="categoryID" class="auth__select" onchange="this.form.submit()" required>
|
||||||
<option value="">Kategorie waehlen</option>
|
<option value="">Kategorie wählen</option>
|
||||||
<?php foreach ($categories as $cat): ?>
|
<?php foreach ($categories as $cat): ?>
|
||||||
<option value="<?= $cat['categoryID'] ?>"
|
<option value="<?= $cat['categoryID'] ?>"
|
||||||
<?= $cat['categoryID'] === $categoryID ? 'selected' : '' ?>>
|
<?= $cat['categoryID'] === $categoryID ? 'selected' : '' ?>>
|
||||||
@ -151,12 +170,26 @@ include 'header.php';
|
|||||||
<?php if ($categoryID > 0): ?>
|
<?php if ($categoryID > 0): ?>
|
||||||
<div class="auth__card">
|
<div class="auth__card">
|
||||||
<header class="auth__header">
|
<header class="auth__header">
|
||||||
<h2 class="auth__title">Produkt hinzufuegen</h2>
|
<h2 class="auth__title">Produkt hinzufügen</h2>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<form method="post" class="auth__form">
|
<form method="post" class="auth__form">
|
||||||
<input type="hidden" name="categoryID" value="<?= $categoryID ?>">
|
<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>
|
<label for="model">Modell</label>
|
||||||
<input id="model" type="text" name="model" class="auth__input" required>
|
<input id="model" type="text" name="model" class="auth__input" required>
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user