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
|
<?php
|
||||||
if (
|
// PRÜFEN: POST-Request und Nutzer ist eingeloggt
|
||||||
$_SERVER['REQUEST_METHOD'] === 'POST' &&
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id'])) {
|
||||||
isset($_POST['add_wishlist']) &&
|
|
||||||
isset($_SESSION['user_id'])
|
|
||||||
) {
|
|
||||||
|
|
||||||
if (!$alreadyInWishlist) {
|
|
||||||
|
|
||||||
$stmtFav = mysqli_prepare(
|
|
||||||
$conn,
|
|
||||||
"INSERT INTO userFavorites (productID, userID) VALUES (?, ?)"
|
|
||||||
);
|
|
||||||
|
|
||||||
|
// FALL 1: Zur Wunschliste hinzufügen
|
||||||
|
if (isset($_POST['add_wishlist']) && !$alreadyInWishlist) {
|
||||||
|
$stmtFav = mysqli_prepare($conn, "INSERT INTO userFavorites (productID, userID) VALUES (?, ?)");
|
||||||
if ($stmtFav) {
|
if ($stmtFav) {
|
||||||
mysqli_stmt_bind_param(
|
mysqli_stmt_bind_param($stmtFav, 'ii', $productId, $_SESSION['user_id']);
|
||||||
$stmtFav,
|
|
||||||
'ii',
|
|
||||||
$productId,
|
|
||||||
$_SESSION['user_id']
|
|
||||||
);
|
|
||||||
|
|
||||||
mysqli_stmt_execute($stmtFav);
|
mysqli_stmt_execute($stmtFav);
|
||||||
mysqli_stmt_close($stmtFav);
|
mysqli_stmt_close($stmtFav);
|
||||||
|
|
||||||
|
// Status aktualisieren, damit gleich der "Entfernen"-Button erscheint
|
||||||
$alreadyInWishlist = true;
|
$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
|
<?php
|
||||||
@ -159,10 +159,18 @@ if ($checkResult->num_rows === 0) {
|
|||||||
|
|
||||||
<?php if (isset($_SESSION['user_id'])): ?>
|
<?php if (isset($_SESSION['user_id'])): ?>
|
||||||
<?php if ($alreadyInWishlist): ?>
|
<?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">
|
<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>
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
|
|
||||||
<form method="POST">
|
<form method="POST">
|
||||||
<input type="hidden" name="product_id" value="<?= (int)$productId ?>">
|
<input type="hidden" name="product_id" value="<?= (int)$productId ?>">
|
||||||
<input type="hidden" name="add_wishlist" value="1">
|
<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">
|
<input class="auth__submit" type="submit" value="Zur Wunschliste hinzufügen">
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<div class="auth__actions">
|
<div class="auth__actions">
|
||||||
<a href="login.php">
|
<a href="login.php">
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user