Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
a250189b08
118
productpage.php
118
productpage.php
@ -358,54 +358,79 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
|||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<?php
|
|
||||||
// Prüfen, ob eine Bewertung abgeschickt wurde und der Nutzer eingeloggt ist
|
|
||||||
if (
|
|
||||||
$_SERVER['REQUEST_METHOD'] === 'POST' &&
|
|
||||||
isset($_POST['submit_review']) &&
|
|
||||||
isset($_SESSION['user_id'])
|
|
||||||
) {
|
|
||||||
// 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",
|
|
||||||
$userID,
|
|
||||||
$productId,
|
|
||||||
$rating,
|
|
||||||
$comment
|
|
||||||
);
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
|
|
||||||
<div class="review-add">
|
<div class="review-add">
|
||||||
<h2 class="reviews-title">Füge deine Bewertung hinzu!</h2>
|
<h2 class="reviews-title">Füge deine Bewertung hinzu!</h2>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
$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']) &&
|
||||||
|
!$userHasReviewed
|
||||||
|
) {
|
||||||
|
$rating = (int)$_POST['rating'];
|
||||||
|
$comment = trim($_POST['comment']);
|
||||||
|
$userID = $_SESSION['user_id'];
|
||||||
|
|
||||||
|
if ($rating >= 1 && $rating <= 5 && !empty($comment)) {
|
||||||
|
|
||||||
|
$stmtInsertRev = mysqli_prepare(
|
||||||
|
$conn,
|
||||||
|
"INSERT INTO reviews (userID, productID, rating, comment) VALUES (?, ?, ?, ?)"
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($stmtInsertRev) {
|
||||||
|
mysqli_stmt_bind_param(
|
||||||
|
$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">
|
<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">
|
<form class="review-input-form" method="post" autocomplete="off">
|
||||||
<input type="hidden" name="submit_review" value="1">
|
<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">
|
<input class="auth__submit" type="submit" value="Senden">
|
||||||
</form>
|
</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; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user