I am developing a shopping cart system and everything is working perfectly, except for one thing.
Well, I use PHP SESSION to store the data for each product, including ID, name, price and quantity. All variables are correctly completed, unless the price variable. I do not know why! I use jQuery to fetch the values and send them to the page that will process them in the SESSION.
I will put the relevant parts of my code to facilitate. This is the content of my products.php:
<?php
session_start();
?>
<html>
<body>
<?php
$connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$sql = "SELECT id, name, price FROM products ORDER BY id";
$result = $connection->query($sql);
if ($result->num_rows > 0)
{
echo "<table>";
echo " <tr>";
echo " <th>ID</th>";
echo " <th>NAME</th>";
echo " <th>PRICE</th>";
echo " <th>QUANTITY</th>";
echo " </tr>";
while($row = $result->fetch_assoc())
{
echo " <tr>";
echo " <td><div class='id-product'>". $row["id"]."</div></td>";
echo " <td><div class='name-product'>". $row["name"]."</div></td>";
echo " <td>$<div class='price-product'>". $row["price"]."</div></td>";
echo " <form class='add'>";
echo " <td><input type='number' name='quantity' value='1' min='1'/></td>";
echo " <td><button type='submit'>Add</button></td>";
echo " </form>";
echo " </tr>";
}
echo "</table>";
}
$connection->close();
?>
<script>
$(document).ready(function()
{
$('.add').on('submit', function()
{
var id = $(this).closest('tr').find('.id-product').text();
var name = $(this).closest('tr').find('.name-product').text();
var price = $(this).closest('tr').find('.price-product').val();
var quantity = $(this).closest('tr').find('input').val();
window.location.href = "add.php?id=" + id + "&name=" + name + "&price=" + price + "&quantity=" + quantity;
return false;
});
});
</script>
<body>
</html>
Now in add.php I have the following:
<?php
session_start();
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$price = isset($_GET['price']) ? $_GET['price'] : "";
$quantity = isset($_GET['quantity']) ? $_GET['quantity'] : "";
$cart_item = array
(
'id' => $id,
'name' => $name,
'price' => $price,
'quantity' => $quantity
);
if(!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
if(array_key_exists($id, $_SESSION['cart']))
{
header('Location: products.php?action=exists&id=' . $id . '&name=' . $name);
}
else if($quantity <= 0)
{
header('Location: products.php?action=invalidquantity&id=' . $id . '&name=' . $name);
}
else
{
$_SESSION['cart'][$id] = $cart_item;
header('Location: products.php?action=added&id=' . $id . '&name=' . $name);
}
?>
To show the products that exists in the SESSION, I use foreach in my page cart.php:
$total = 0;
foreach($_SESSION['cart'] as $id)
{
echo "<tr>";
echo "<td>{$id['name']}</td>";
echo "<td>${$id['price']}</td>";
echo "<td>{$id['quantity']}</td>";
echo "<td>";
echo "<a href='remove.php?id={$id['id']}&name={$id['name']}'>Remove</a>";
echo "</td>";
echo "</tr>";
$total += ($id['price'] * $id['quantity']);
}
As I said earlier, I have returned the values of ID, name and quantity, but the price remains empty. What's wrong?