Soo I want to be able to update my cart table in mysql database, when you click plus minus button in the cart of my ecommerce website. I want to be able to do this with session_id(), so no login and no cookies. Is this possible?
I tried this:
I also have a page called add_to_cart.php in which i set session_id() and update quantity everytime someone clicks on “add to cart”
<body>
<h1>Your Cart</h1>
<div class="cart-container" id="cart-container">
<div class="cart">
<?php while ($row = $result->fetch_assoc()): ?>
<div class="cart-item" id="cart-item" data-product-id=<?php echo $row['product_id'];?>>
<h2><?php echo $row['productname']; ?></h2>
<img id="img" style="border-radius:20px;width:150px" src="data:image/jpg;charset=utf;base64, <?php echo base64_encode($row["productimage"]); ?>"/>
<p>Price: $<?php echo $row['price']; ?></p>
<div class="quantity-selector"><p>Quantity:</p>
<button class="minus-btn" onclick="decrement(this)">-</button>
<input type="number" id="quantity" class="quantity" value="1" min="1">
<button class="plus-btn" onclick="increment(this)">+</button>
</div>
<p>Quantity: <?php echo $row['quantity']; ?></p>
<span class="quantity">1</span>
<p>Subtotal: $<?php echo $row['price'] * $row['quantity']; ?></p>
</div>
<?php endwhile; ?>
</div>
</div>
<script>
var item= document.getElementsByClassName("cart-item");
function increment(element) {
//var quantityInput = document.getElementById('quantity');
let quantityInput = element.closest("div.cart-item").querySelector(".quantity");
var productId = element.closest("div.cart-item").getAttribute('data-product-id');
var currentValue = parseInt(quantityInput.value);
//var quantitySpan = element.closest("div.cart-item").querySelector('.quantity');
quantityInput.value = currentValue + 1;
//var newQuantity = parseInt(quantitySpan.innerText) + 1;
//updateQuantity(element.closest("div.cart-item").dataset.itemId, quantityInput.value);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'update_cart.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// Update the quantity in the UI
//quantityInput.innerText = quantityInput.value;
quantityInput.innerText = quantityInput.value;
}
};
xhr.send('product_id=' + productId + '&quantity=' + quantityInput.value);
}
function decrement(element) {
//var quantityInput = document.getElementById('quantity');
let quantityInput = element.closest("div.cart-item").querySelector(".quantity");
var productId = element.closest("div.cart-item").getAttribute('data-product-id');
var currentValue = parseInt(quantityInput.value);
if (currentValue > 1) {
quantityInput.value = currentValue - 1;
}
//updateQuantity(element.closest("div.cart-item").dataset.itemId, quantityInput.value);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'update_cart.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// Update the quantity in the UI
quantityInput.innerText = quantityInput.value;
}
};
xhr.send('product_id=' + productId + '&quantity=' + quantityInput.value);
}
and this is my 'update_cart.php' file:
<?php
session_start();
if (!isset($_SESSION['user_session'])) {
// Generate a new session id if not already set
$_SESSION['user_session'] = session_id();
}
$session_id = $_SESSION['user_session'];
$product_id = $_POST['product_id'];
$new_quantity = $_POST['quantity'];
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "luciavioleta";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Update quantity in the database
$sql = "UPDATE cart SET quantity = ? WHERE user_session = ? AND product_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("isi", $new_quantity, $session_id, $product_id);
if ($stmt->execute()) {
echo "Quantity updated successfully";
} else {
echo "Error updating quantity: " . $conn->error;
}
$stmt->close();
$conn->close();
?>
New contributor
Luzzi98 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.