Implement wishlist functionality: add and remove items from userFavorites
This commit is contained in:
parent
391f0a9853
commit
d9ac63b647
@ -94,36 +94,36 @@ if ($checkResult->num_rows === 0) {
|
||||
}
|
||||
?>
|
||||
|
||||
<?php
|
||||
if (
|
||||
$_SERVER['REQUEST_METHOD'] === 'POST' &&
|
||||
isset($_POST['add_wishlist']) &&
|
||||
isset($_SESSION['user_id'])
|
||||
) {
|
||||
|
||||
if (!$alreadyInWishlist) {
|
||||
|
||||
$stmtFav = mysqli_prepare(
|
||||
$conn,
|
||||
"INSERT INTO userFavorites (productID, userID) VALUES (?, ?)"
|
||||
);
|
||||
<?php
|
||||
// PRÜFEN: POST-Request und Nutzer ist eingeloggt
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id'])) {
|
||||
|
||||
// FALL 1: Zur Wunschliste hinzufügen
|
||||
if (isset($_POST['add_wishlist']) && !$alreadyInWishlist) {
|
||||
$stmtFav = mysqli_prepare($conn, "INSERT INTO userFavorites (productID, userID) VALUES (?, ?)");
|
||||
if ($stmtFav) {
|
||||
mysqli_stmt_bind_param(
|
||||
$stmtFav,
|
||||
'ii',
|
||||
$productId,
|
||||
$_SESSION['user_id']
|
||||
);
|
||||
|
||||
mysqli_stmt_bind_param($stmtFav, 'ii', $productId, $_SESSION['user_id']);
|
||||
mysqli_stmt_execute($stmtFav);
|
||||
mysqli_stmt_close($stmtFav);
|
||||
|
||||
// Status aktualisieren, damit gleich der "Entfernen"-Button erscheint
|
||||
$alreadyInWishlist = true;
|
||||
}
|
||||
}
|
||||
// FALL 2: Von der Wunschliste entfernen (NEU)
|
||||
elseif (isset($_POST['remove_wishlist']) && $alreadyInWishlist) {
|
||||
$stmtDel = mysqli_prepare($conn, "DELETE FROM userFavorites WHERE productID = ? AND userID = ?");
|
||||
if ($stmtDel) {
|
||||
mysqli_stmt_bind_param($stmtDel, 'ii', $productId, $_SESSION['user_id']);
|
||||
mysqli_stmt_execute($stmtDel);
|
||||
mysqli_stmt_close($stmtDel);
|
||||
|
||||
// Status aktualisieren, damit gleich wieder der "Hinzufügen"-Button erscheint
|
||||
$alreadyInWishlist = false;
|
||||
}
|
||||
?>
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
<?php
|
||||
@ -159,10 +159,18 @@ if ($checkResult->num_rows === 0) {
|
||||
|
||||
<?php if (isset($_SESSION['user_id'])): ?>
|
||||
<?php if ($alreadyInWishlist): ?>
|
||||
|
||||
<form method="POST">
|
||||
<input type="hidden" name="product_id" value="<?= (int)$productId ?>">
|
||||
<input type="hidden" name="remove_wishlist" value="1">
|
||||
|
||||
<div class="auth__actions">
|
||||
<input class="auth__submit" type="button" value="Bereits in Wunschliste" disabled>
|
||||
<input class="auth__submit" type="submit" value="Aus Wunschliste entfernen" style="background: #ef4444; border-color: #ef4444; color: white;">
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php else: ?>
|
||||
|
||||
<form method="POST">
|
||||
<input type="hidden" name="product_id" value="<?= (int)$productId ?>">
|
||||
<input type="hidden" name="add_wishlist" value="1">
|
||||
@ -170,7 +178,9 @@ if ($checkResult->num_rows === 0) {
|
||||
<input class="auth__submit" type="submit" value="Zur Wunschliste hinzufügen">
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php endif; ?>
|
||||
|
||||
<?php else: ?>
|
||||
<div class="auth__actions">
|
||||
<a href="login.php">
|
||||
|
||||
Loading…
Reference in New Issue
Block a user