Geizkragen/productpage.php

195 lines
5.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php include 'header.php'; ?>
<?php
// login.php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
// 1) DB-Verbindung (einmal)
$servername = "localhost";
$port = 3306;
$username = "FSST";
$password = "L9wUNZZ9Qkbt";
$db = "FSST";
$conn = mysqli_connect($servername, $username, $password, $db, $port);
if (!$conn)
{
http_response_code(500);
die("Datenbankfehler");
}
?>
<?php
$productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
?>
<?php if ($productId <= 0): ?>
<section class="product-section">
<h2>Produkt nicht gefunden</h2>
<p>Bitte eine gueltige Produkt-ID mitgeben.</p>
</section>
<?php else: ?>
<?php
$stmt = $conn->prepare("
SELECT
a.name,
a.unit,
a.dataType,
pa.valueString,
pa.valueNumber,
pa.valueBool,
p.model,
p.description,
p.imagePath
FROM products p
INNER JOIN categoryAttributes ca
ON p.categoryID = ca.categoryID
INNER JOIN attributes a
ON ca.attributeID = a.attributeID
LEFT JOIN productAttributes pa
ON pa.productID = p.productID
AND pa.attributeID = a.attributeID
WHERE p.productID = ?
ORDER BY a.attributeID
");
$stmt->bind_param("i", $productId);
$stmt->execute();
$result = $stmt->get_result();
$product = $result->fetch_assoc();
?>
<?php
$alreadyInWishlist = null;
if (
$_SERVER['REQUEST_METHOD'] === 'POST' &&
isset($_POST['add_wishlist']) &&
isset($_SESSION['user_id'])
) {
$userId = (int)$_SESSION['user_id'];
$productIdPost = (int)$_POST['product_id'];
if ($productIdPost > 0 && $alreadyInWishlist == null) {
$stmtFav = mysqli_prepare(
$conn,
"INSERT IGNORE INTO userFavorites (productID, userID) VALUES (?, ?)"
);
if ($stmtFav) {
mysqli_stmt_bind_param($stmtFav, 'ii', $productIdPost, $userId);
mysqli_stmt_execute($stmtFav);
mysqli_stmt_close($stmtFav);
$alreadyInWishlist = true;
}
}
}
?>
<div class="product-wrapper">
<!-- LINKER BEREICH BILD -->
<div class="product-left">
<div class="product-image-box">
<img
src="<?= isset($product['imagePath']) ? $product['imagePath'] : 'assets/images/placeholder.png' ?>"
alt="<?= htmlspecialchars($product['model'] ?? 'Produktbild') ?>">
</div>
<?php if (isset($_SESSION['user_id'])): ?>
<?php if ($alreadyInWishlist): ?>
<div class="auth__actions">
<input class="auth__submit" type="button"
value="Bereits in Wunschliste"
disabled>
</div>
<?php else: ?>
<form method="POST">
<input type="hidden" name="product_id" value="<?= (int)$productId ?>">
<input type="hidden" name="add_wishlist" value="1">
<div class="auth__actions">
<input class="auth__submit"
type="submit"
value="Zur Wunschliste hinzufügen">
</div>
</form>
<?php endif; ?>
<?php else: ?>
<div class="auth__actions">
<a href="login.php">
<input class="auth__submit" type="button"
value="Zum Hinzufügen einloggen">
</a>
</div>
<?php endif; ?>
</div>
<!-- RECHTER BEREICH DETAILS -->
<div class="product-right">
<h1 class="product-title">
<?= htmlspecialchars($product['model'] ?? 'Produkt') ?>
</h1>
<div class="product-specs">
<div class="product-desc">
<?= htmlspecialchars($product['description']) ?>
</div>
<?php
while ($row = $result->fetch_assoc()) {
echo "<p><strong>{$row['name']}:</strong> ";
if (!empty($row['valueString'])) echo $row['valueString'];
if (!empty($row['valueNumber'])) echo $row['valueNumber'] . " " . $row['unit'];
if (!is_null($row['valueBool'])) echo $row['valueBool'] ? "Ja" : "Nein";
echo "</p>";
}
?>
</div>
</div>
</div>
<div class ="shop-offers"
<?php $stmt->close(); ?>
<?php endif; ?>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="assets/css/productpage.css">
</head>
<?php include 'footer.php'; ?>