Review adder made limit 1 review per user

This commit is contained in:
Paul Eisenbock 2026-03-18 15:43:27 +01:00
parent 9a3cdd975a
commit cf7d60708e

View File

@ -362,11 +362,27 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
<h2 class="reviews-title">Füge deine Bewertung hinzu!</h2> <h2 class="reviews-title">Füge deine Bewertung hinzu!</h2>
<?php <?php
// Die Logik bleibt hier unten im Block! $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 ( if (
$_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['REQUEST_METHOD'] === 'POST' &&
isset($_POST['submit_review']) && isset($_POST['submit_review']) &&
isset($_SESSION['user_id']) isset($_SESSION['user_id']) &&
!$userHasReviewed
) { ) {
$rating = (int)$_POST['rating']; $rating = (int)$_POST['rating'];
$comment = trim($_POST['comment']); $comment = trim($_POST['comment']);
@ -392,7 +408,7 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
mysqli_stmt_execute($stmtInsertRev); mysqli_stmt_execute($stmtInsertRev);
mysqli_stmt_close($stmtInsertRev); mysqli_stmt_close($stmtInsertRev);
// TRICK: JavaScript-Weiterleitung anstelle von PHP-Header! // JS Weiterleitung
echo "<script>window.location.href = 'productpage.php?id=" . $productId . "';</script>"; echo "<script>window.location.href = 'productpage.php?id=" . $productId . "';</script>";
exit; exit;
} }
@ -400,107 +416,49 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
} }
?> ?>
<div class="review-add"> <div class="review-card">
<h2 class="reviews-title">Füge deine Bewertung hinzu!</h2> <?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 <?php elseif ($userHasReviewed): ?>
$userHasReviewed = false; <div class="review-login-prompt">
<p class="review-login-msg">Du hast dieses Produkt bereits bewertet. Vielen Dank!</p>
</div>
// 1. Prüfen, ob der eingeloggte Nutzer schon bewertet hat <?php else: ?>
if (isset($_SESSION['user_id'])) { <form class="review-input-form" method="post" autocomplete="off">
$stmtCheckRev = mysqli_prepare($conn, "SELECT 1 FROM reviews WHERE userID = ? AND productID = ? LIMIT 1"); <input type="hidden" name="submit_review" value="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) { <div class="rating-input">
$userHasReviewed = true; <input type="radio" id="star5" name="rating" value="5" required />
} <label for="star5" title="5 Sterne"></label>
mysqli_stmt_close($stmtCheckRev);
}
// 2. Bewertung speichern (NUR wenn noch keine existiert!) <input type="radio" id="star4" name="rating" value="4" />
if ( <label for="star4" title="4 Sterne"></label>
$_SERVER['REQUEST_METHOD'] === 'POST' &&
isset($_POST['submit_review']) &&
isset($_SESSION['user_id']) &&
!$userHasReviewed
) {
$rating = (int)$_POST['rating'];
$comment = trim($_POST['comment']);
$userID = $_SESSION['user_id'];
if ($rating >= 1 && $rating <= 5 && !empty($comment)) { <input type="radio" id="star3" name="rating" value="3" />
<label for="star3" title="3 Sterne"></label>
$stmtInsertRev = mysqli_prepare( <input type="radio" id="star2" name="rating" value="2" />
$conn, <label for="star2" title="2 Sterne"></label>
"INSERT INTO reviews (userID, productID, rating, comment) VALUES (?, ?, ?, ?)"
);
if ($stmtInsertRev) { <input type="radio" id="star1" name="rating" value="1" />
mysqli_stmt_bind_param( <label for="star1" title="1 Stern"></label>
$stmtInsertRev,
"iiis",
$userID,
$productId,
$rating,
$comment
);
mysqli_stmt_execute($stmtInsertRev);
mysqli_stmt_close($stmtInsertRev);
// JS Weiterleitung
echo "<script>window.location.href = 'productpage.php?id=" . $productId . "';</script>";
exit;
}
}
}
?>
<div class="review-card">
<?php if (!isset($_SESSION['user_id'])): ?>
<div class="review-login-prompt">
<p class="review-login-msg">Du musst eingeloggt sein, um eine Bewertung abzugeben.</p>
<a href="login.php">
<input class="auth__submit" type="button" value="Zum Einloggen">
</a>
</div> </div>
<?php elseif ($userHasReviewed): ?> <textarea class="review-comment-input" name="comment" rows="4"
<div class="review-login-prompt"> placeholder="Teile deine Meinung mit anderen!" required></textarea>
<p class="review-login-msg">Du hast dieses Produkt bereits bewertet. Vielen Dank!</p>
</div>
<?php else: ?> <input class="auth__submit" type="submit" value="Senden">
<form class="review-input-form" method="post" autocomplete="off"> </form>
<input type="hidden" name="submit_review" value="1"> <?php endif; ?>
<div class="rating-input">
<input type="radio" id="star5" name="rating" value="5" required />
<label for="star5" title="5 Sterne"></label>
<input type="radio" id="star4" name="rating" value="4" />
<label for="star4" title="4 Sterne"></label>
<input type="radio" id="star3" name="rating" value="3" />
<label for="star3" title="3 Sterne"></label>
<input type="radio" id="star2" name="rating" value="2" />
<label for="star2" title="2 Sterne"></label>
<input type="radio" id="star1" name="rating" value="1" />
<label for="star1" title="1 Stern"></label>
</div>
<textarea class="review-comment-input" name="comment" rows="4"
placeholder="Teile deine Meinung mit anderen!" required></textarea>
<input class="auth__submit" type="submit" value="Senden">
</form>
<?php endif; ?>
</div>
</div> </div>
</div>
<?php endif; ?> <?php endif; ?>
<?php include 'footer.php'; ?> <?php include 'footer.php'; ?>