What are the potential pitfalls of using arrays in PHP sessions for online shopping carts?

One potential pitfall of using arrays in PHP sessions for online shopping carts is that it can lead to scalability issues as the array grows larger with each added item. To solve this issue, it is recommended to store shopping cart items in a database instead of using arrays in sessions.

// Instead of storing shopping cart items in a session array, store them in a database
// Example code to add an item to the shopping cart in the database
$productId = 1;
$quantity = 2;

// Connect to the database
$pdo = new PDO("mysql:host=localhost;dbname=your_database", "username", "password");

// Check if the item already exists in the cart
$stmt = $pdo->prepare("SELECT * FROM shopping_cart WHERE product_id = :product_id");
$stmt->bindParam(':product_id', $productId);
$stmt->execute();
$item = $stmt->fetch();

if ($item) {
    // Update the quantity of the existing item
    $newQuantity = $item['quantity'] + $quantity;
    $stmt = $pdo->prepare("UPDATE shopping_cart SET quantity = :quantity WHERE product_id = :product_id");
    $stmt->bindParam(':quantity', $newQuantity);
    $stmt->bindParam(':product_id', $productId);
    $stmt->execute();
} else {
    // Add a new item to the cart
    $stmt = $pdo->prepare("INSERT INTO shopping_cart (product_id, quantity) VALUES (:product_id, :quantity)");
    $stmt->bindParam(':product_id', $productId);
    $stmt->bindParam(':quantity', $quantity);
    $stmt->execute();
}