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> </div>
<?php <?php
// Unterschiedliche DB-Stände: URL-Spalte heißt je nach Schema z.B. productURL oder 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. // Wir ermitteln die existierende Spalte dynamisch, damit die Seite nicht mit "Unknown column" crasht.
$urlColumn = ''; $urlColumn = '';
$colCheck = mysqli_query($conn, "SHOW COLUMNS FROM offers LIKE 'productURL'"); $colCheck = mysqli_query($conn, "SHOW COLUMNS FROM offers LIKE 'productURL'");
if ($colCheck && mysqli_num_rows($colCheck) > 0) { if ($colCheck && mysqli_num_rows($colCheck) > 0) {
$urlColumn = 'productURL'; $urlColumn = 'productURL';
} else { } else {
$colCheck2 = mysqli_query($conn, "SHOW COLUMNS FROM offers LIKE 'offerURL'"); $colCheck2 = mysqli_query($conn, "SHOW COLUMNS FROM offers LIKE 'offerURL'");
if ($colCheck2 && mysqli_num_rows($colCheck2) > 0) { if ($colCheck2 && mysqli_num_rows($colCheck2) > 0) {
$urlColumn = 'offerURL'; $urlColumn = 'offerURL';
}
} }
}
$urlSelect = $urlColumn !== '' ? ("offers." . $urlColumn . " AS offerURL") : "'' AS offerURL"; $urlSelect = $urlColumn !== '' ? ("offers." . $urlColumn . " AS offerURL") : "'' AS offerURL";
$stmt = mysqli_prepare($conn, $stmt = mysqli_prepare($conn,
"SELECT price, shippingCost, inStock, shops.name, $urlSelect, shops.logoPath, shops.shippingTime "SELECT price, shippingCost, inStock, shops.name, $urlSelect, shops.logoPath, shops.shippingTime
FROM offers FROM offers
INNER JOIN shops ON INNER JOIN shops ON
offers.shopID = shops.shopID WHERE offers.productID = ? ORDER BY offers.price ASC"); offers.shopID = shops.shopID WHERE offers.productID = ? ORDER BY offers.price ASC");
$stmt->bind_param("i", $productId); $stmt->bind_param("i", $productId);
$stmt->execute(); $stmt->execute();
$result = $stmt->get_result(); $result = $stmt->get_result();
$shopInfo = []; $shopInfo = [];
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
$shopInfo[] = $row; $shopInfo[] = $row;
} }
?> ?>
@ -319,10 +319,12 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
<?php <?php
// HIER ANGEPASST: profilePicture und createdAt zum SELECT hinzugefügt
$stmt = mysqli_prepare($conn, $stmt = mysqli_prepare($conn,
" SELECT rating, comment, users.displayname FROM reviews " SELECT rating, comment, users.displayname, users.profilePicture, reviews.createdAt
INNER JOIN users ON reviews.userID = users.userID FROM reviews
WHERE productID = ? ORDER BY rating DESC"); INNER JOIN users ON reviews.userID = users.userID
WHERE productID = ? ORDER BY rating DESC");
$stmt->bind_param("i", $productId); $stmt->bind_param("i", $productId);
$stmt->execute(); $stmt->execute();
@ -338,146 +340,156 @@ $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
<div class="reviews"> <div class="reviews">
<h2 class="reviews-title">Bewertungen</h2> <h2 class="reviews-title">Bewertungen</h2>
<div class="reviews-all"> <div class="reviews-all">
<?php if (!empty($reviews)): ?> <?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-header">
<div class="review-user">
<?= htmlspecialchars($review['displayname']) ?>
</div>
<div class="review-rating"> <div class="review-user-info">
<?php for ($i = 1; $i <= 5; $i++): ?> <img class="review-avatar"
<span class="star <?= $i <= $review['rating'] ? 'filled' : '' ?>"></span> src="<?= !empty($review['profilePicture']) ? htmlspecialchars($review['profilePicture']) : 'assets/images/placeholder.png' ?>"
<?php endfor; ?> 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> </div>
<div class="review-comment"> <div class="review-rating">
<?= nl2br(htmlspecialchars($review['comment'])) ?> <?php for ($i = 1; $i <= 5; $i++): ?>
<span class="star <?= $i <= $review['rating'] ? 'filled' : '' ?>"></span>
<?php endfor; ?>
</div> </div>
</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> </div>
<?php endif; ?>
</div>
<div class="review-add"> <?php endforeach; ?>
<h2 class="reviews-title">Füge deine Bewertung hinzu!</h2>
<?php <?php else: ?>
$userHasReviewed = false; <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 <div class="review-add">
if (isset($_SESSION['user_id'])) { <h2 class="reviews-title">Füge deine Bewertung hinzu!</h2>
$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) { <?php
$userHasReviewed = true; $userHasReviewed = false;
}
mysqli_stmt_close($stmtCheckRev); // 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!) // 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 !$userHasReviewed
) { ) {
$rating = (int)$_POST['rating']; $rating = (int)$_POST['rating'];
$comment = trim($_POST['comment']); $comment = trim($_POST['comment']);
$userID = $_SESSION['user_id']; $userID = $_SESSION['user_id'];
if ($rating >= 1 && $rating <= 5 && !empty($comment)) { if ($rating >= 1 && $rating <= 5 && !empty($comment)) {
$stmtInsertRev = mysqli_prepare( $stmtInsertRev = mysqli_prepare(
$conn, $conn,
"INSERT INTO reviews (userID, productID, rating, comment) VALUES (?, ?, ?, ?)" "INSERT INTO reviews (userID, productID, rating, comment) VALUES (?, ?, ?, ?)"
);
if ($stmtInsertRev) {
mysqli_stmt_bind_param(
$stmtInsertRev,
"iiis",
$userID,
$productId,
$rating,
$comment
); );
if ($stmtInsertRev) { mysqli_stmt_execute($stmtInsertRev);
mysqli_stmt_bind_param( mysqli_stmt_close($stmtInsertRev);
$stmtInsertRev,
"iiis",
$userID,
$productId,
$rating,
$comment
);
mysqli_stmt_execute($stmtInsertRev); // JS Weiterleitung
mysqli_stmt_close($stmtInsertRev); 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"> <div class="review-card">
<?php if (!isset($_SESSION['user_id'])): ?> <?php if (!isset($_SESSION['user_id'])): ?>
<div class="review-login-prompt"> <div class="review-login-prompt">
<p style="color: #cbd5e1; margin-bottom: 1rem;">Du musst eingeloggt sein, um eine Bewertung abzugeben.</p> <p style="color: #cbd5e1; margin-bottom: 1rem;">Du musst eingeloggt sein, um eine Bewertung abzugeben.</p>
<a href="login.php"> <a href="login.php">
<input class="auth__submit" type="button" value="Zum Einloggen"> <input class="auth__submit" type="button" value="Zum Einloggen">
</a> </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> </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'; ?>