Merge remote-tracking branch 'origin/main'

This commit is contained in:
Fabian Schieder 2026-03-18 16:02:13 +01:00
commit a250189b08

View File

@ -358,29 +358,44 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
<?php endif; ?>
</div>
<div class="review-add">
<h2 class="reviews-title">Füge deine Bewertung hinzu!</h2>
<?php
// Prüfen, ob eine Bewertung abgeschickt wurde und der Nutzer eingeloggt ist
$userHasReviewed = false;
// 1. Prüfen, ob der eingeloggte Nutzer schon bewertet hat
if (isset($_SESSION['user_id'])) {
$stmtCheckRev = mysqli_prepare($conn, "SELECT 1 FROM reviews WHERE userID = ? AND productID = ? LIMIT 1");
mysqli_stmt_bind_param($stmtCheckRev, "ii", $_SESSION['user_id'], $productId);
mysqli_stmt_execute($stmtCheckRev);
mysqli_stmt_store_result($stmtCheckRev);
if (mysqli_stmt_num_rows($stmtCheckRev) > 0) {
$userHasReviewed = true;
}
mysqli_stmt_close($stmtCheckRev);
}
// 2. Bewertung speichern (NUR wenn noch keine existiert!)
if (
$_SERVER['REQUEST_METHOD'] === 'POST' &&
isset($_POST['submit_review']) &&
isset($_SESSION['user_id'])
isset($_SESSION['user_id']) &&
!$userHasReviewed
) {
// Daten aus dem Formular holen und absichern
$rating = (int)$_POST['rating'];
$comment = trim($_POST['comment']);
$userID = $_SESSION['user_id'];
// Kleine Validierung: Ist das Rating gültig und der Kommentar nicht leer?
if ($rating >= 1 && $rating <= 5 && !empty($comment)) {
// SQL-Statement vorbereiten (wie im Screenshot: userID, productID, rating, comment)
$stmtInsertRev = mysqli_prepare(
$conn,
"INSERT INTO reviews (userID, productID, rating, comment) VALUES (?, ?, ?, ?)"
);
if ($stmtInsertRev) {
// "iiis" steht für: Integer, Integer, Integer, String
mysqli_stmt_bind_param(
$stmtInsertRev,
"iiis",
@ -393,19 +408,29 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
mysqli_stmt_execute($stmtInsertRev);
mysqli_stmt_close($stmtInsertRev);
// WICHTIG: Die Seite neu laden, damit das Formular bei einem Refresh (F5) nicht doppelt gesendet wird
header("Location: productpage.php?id=" . $productId);
exit; // Stoppt das Skript hier, da wir weiterleiten
// JS Weiterleitung
echo "<script>window.location.href = 'productpage.php?id=" . $productId . "';</script>";
exit;
}
}
}
?>
<div class="review-add">
<h2 class="reviews-title">Füge deine Bewertung hinzu!</h2>
<div class="review-card">
<?php if (isset($_SESSION['user_id'])): ?>
<?php if (!isset($_SESSION['user_id'])): ?>
<div class="review-login-prompt">
<p style="color: #cbd5e1; margin-bottom: 1rem;">Du musst eingeloggt sein, um eine Bewertung abzugeben.</p>
<a href="login.php">
<input class="auth__submit" type="button" value="Zum Einloggen">
</a>
</div>
<?php elseif ($userHasReviewed): ?>
<div class="review-login-prompt">
<p class="review-login-msg">Du hast dieses Produkt bereits bewertet. Vielen Dank!</p>
</div>
<?php else: ?>
<form class="review-input-form" method="post" autocomplete="off">
<input type="hidden" name="submit_review" value="1">
@ -431,11 +456,6 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
<input class="auth__submit" type="submit" value="Senden">
</form>
<?php else: ?>
<p style="color: #cbd5e1; margin-bottom: 1rem;" >Du musst eingeloggt sein, um eine Bewertung abzugeben.</p>
<a href="login.php">
<input class="auth__submit" type="button" value="Zum Einloggen">
</a>
<?php endif; ?>
</div>
</div>