Profilepictuers for reviews added

This commit is contained in:
Paul Eisenbock 2026-03-18 16:19:06 +01:00
parent 5cb8badc23
commit 5e68d3b6c5

View File

@ -238,36 +238,36 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
</div>
<?php
// Unterschiedliche DB-Stände: URL-Spalte heißt je nach Schema z.B. productURL oder offerURL.
// Wir ermitteln die existierende Spalte dynamisch, damit die Seite nicht mit "Unknown column" crasht.
$urlColumn = '';
$colCheck = mysqli_query($conn, "SHOW COLUMNS FROM offers LIKE 'productURL'");
if ($colCheck && mysqli_num_rows($colCheck) > 0) {
$urlColumn = 'productURL';
} else {
$colCheck2 = mysqli_query($conn, "SHOW COLUMNS FROM offers LIKE 'offerURL'");
if ($colCheck2 && mysqli_num_rows($colCheck2) > 0) {
$urlColumn = 'offerURL';
}
// Unterschiedliche DB-Stände: URL-Spalte heißt je nach Schema z.B. productURL oder offerURL.
// Wir ermitteln die existierende Spalte dynamisch, damit die Seite nicht mit "Unknown column" crasht.
$urlColumn = '';
$colCheck = mysqli_query($conn, "SHOW COLUMNS FROM offers LIKE 'productURL'");
if ($colCheck && mysqli_num_rows($colCheck) > 0) {
$urlColumn = 'productURL';
} else {
$colCheck2 = mysqli_query($conn, "SHOW COLUMNS FROM offers LIKE 'offerURL'");
if ($colCheck2 && mysqli_num_rows($colCheck2) > 0) {
$urlColumn = 'offerURL';
}
}
$urlSelect = $urlColumn !== '' ? ("offers." . $urlColumn . " AS offerURL") : "'' AS offerURL";
$urlSelect = $urlColumn !== '' ? ("offers." . $urlColumn . " AS offerURL") : "'' AS offerURL";
$stmt = mysqli_prepare($conn,
"SELECT price, shippingCost, inStock, shops.name, $urlSelect, shops.logoPath, shops.shippingTime
$stmt = mysqli_prepare($conn,
"SELECT price, shippingCost, inStock, shops.name, $urlSelect, shops.logoPath, shops.shippingTime
FROM offers
INNER JOIN shops ON
offers.shopID = shops.shopID WHERE offers.productID = ? ORDER BY offers.price ASC");
$stmt->bind_param("i", $productId);
$stmt->execute();
$result = $stmt->get_result();
$stmt->bind_param("i", $productId);
$stmt->execute();
$result = $stmt->get_result();
$shopInfo = [];
$shopInfo = [];
while ($row = $result->fetch_assoc()) {
$shopInfo[] = $row;
}
while ($row = $result->fetch_assoc()) {
$shopInfo[] = $row;
}
?>
@ -319,10 +319,12 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
<?php
// HIER ANGEPASST: profilePicture und createdAt zum SELECT hinzugefügt
$stmt = mysqli_prepare($conn,
" SELECT rating, comment, users.displayname FROM reviews
INNER JOIN users ON reviews.userID = users.userID
WHERE productID = ? ORDER BY rating DESC");
" SELECT rating, comment, users.displayname, users.profilePicture, reviews.createdAt
FROM reviews
INNER JOIN users ON reviews.userID = users.userID
WHERE productID = ? ORDER BY rating DESC");
$stmt->bind_param("i", $productId);
$stmt->execute();
@ -338,146 +340,156 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
<div class="reviews">
<h2 class="reviews-title">Bewertungen</h2>
<div class="reviews-all">
<?php if (!empty($reviews)): ?>
<h2 class="reviews-title">Bewertungen</h2>
<div class="reviews-all">
<?php if (!empty($reviews)): ?>
<?php foreach ($reviews as $review): ?>
<?php foreach ($reviews as $review): ?>
<div class="review-card">
<div class="review-card">
<div class="review-header">
<div class="review-user">
<?= htmlspecialchars($review['displayname']) ?>
</div>
<div class="review-header">
<div class="review-rating">
<?php for ($i = 1; $i <= 5; $i++): ?>
<span class="star <?= $i <= $review['rating'] ? 'filled' : '' ?>"></span>
<?php endfor; ?>
<div class="review-user-info">
<img class="review-avatar"
src="<?= !empty($review['profilePicture']) ? htmlspecialchars($review['profilePicture']) : 'assets/images/placeholder.png' ?>"
alt="Profilbild von <?= htmlspecialchars($review['displayname']) ?>">
<div>
<div class="review-user">
<?= htmlspecialchars($review['displayname']) ?>
</div>
<?php if (isset($review['createdAt'])): ?>
<div style="font-size: 0.8rem; color: #94a3b8;">
<?= date('d.m.Y', strtotime($review['createdAt'])) ?>
</div>
<?php endif; ?>
</div>
</div>
<div class="review-comment">
<?= nl2br(htmlspecialchars($review['comment'])) ?>
<div class="review-rating">
<?php for ($i = 1; $i <= 5; $i++): ?>
<span class="star <?= $i <= $review['rating'] ? 'filled' : '' ?>"></span>
<?php endfor; ?>
</div>
</div>
<?php endforeach; ?>
<div class="review-comment">
<?= nl2br(htmlspecialchars($review['comment'])) ?>
</div>
<?php else: ?>
<div class="no-review">
<p>Es gibt noch keine Bewertungen.</p>
</div>
<?php endif; ?>
</div>
<div class="review-add">
<h2 class="reviews-title">Füge deine Bewertung hinzu!</h2>
<?php endforeach; ?>
<?php
$userHasReviewed = false;
<?php else: ?>
<div class="no-review">
<p>Es gibt noch keine Bewertungen.</p>
</div>
<?php endif; ?>
</div>
// 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);
<div class="review-add">
<h2 class="reviews-title">Füge deine Bewertung hinzu!</h2>
if (mysqli_stmt_num_rows($stmtCheckRev) > 0) {
$userHasReviewed = true;
}
mysqli_stmt_close($stmtCheckRev);
<?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'];
// 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)) {
if ($rating >= 1 && $rating <= 5 && !empty($comment)) {
$stmtInsertRev = mysqli_prepare(
$conn,
"INSERT INTO reviews (userID, productID, rating, comment) VALUES (?, ?, ?, ?)"
$stmtInsertRev = mysqli_prepare(
$conn,
"INSERT INTO reviews (userID, productID, rating, comment) VALUES (?, ?, ?, ?)"
);
if ($stmtInsertRev) {
mysqli_stmt_bind_param(
$stmtInsertRev,
"iiis",
$userID,
$productId,
$rating,
$comment
);
if ($stmtInsertRev) {
mysqli_stmt_bind_param(
$stmtInsertRev,
"iiis",
$userID,
$productId,
$rating,
$comment
);
mysqli_stmt_execute($stmtInsertRev);
mysqli_stmt_close($stmtInsertRev);
mysqli_stmt_execute($stmtInsertRev);
mysqli_stmt_close($stmtInsertRev);
// JS Weiterleitung
echo "<script>window.location.href = 'productpage.php?id=" . $productId . "';</script>";
exit;
}
// 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 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 class="review-card">
<?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">
<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>
<?php elseif ($userHasReviewed): ?>
<div class="review-login-prompt">
<p class="review-login-msg">Du hast dieses Produkt bereits bewertet. Vielen Dank!</p>
</div>
<textarea class="review-comment-input" name="comment" rows="4"
placeholder="Teile deine Meinung mit anderen!" required></textarea>
<?php else: ?>
<form class="review-input-form" method="post" autocomplete="off">
<input type="hidden" name="submit_review" value="1">
<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>
<input class="auth__submit" type="submit" value="Senden">
</form>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php include 'footer.php'; ?>