When products are added to the shopping cart, these errors disappear and do not appear to be serious, since everything flows normally, and I could even deactivate them by doing the following: error_reporting(0);
but on the other hand, I do not think this is the most useful, a code is not should not generate even the slightest error.
Returning to the topic when there are no products added to the cart, it generates two errors, which are the following:
Warning: str_repeat(): Second argument has to be greater than or equal to 0 in
$strMarcas = str_repeat('?,', count($inputItems) - 1) . '?';
Warning: Wrong parameter count for mysqli_stmt::bind_param() in
$stmt->bind_param($strTipos, ...$inputItems);
This is my code:
<?php
session_start();
$items = (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) ? $_SESSION['cart'] : array();
foreach($items as $id_product => $item) {
foreach($item as $color => $sizes) {
foreach($sizes as $size => $quantity) {
$meQty += $quantity;
}
}
}
$inputItems = array_keys($items);
$strMarcas = str_repeat('?,', count($inputItems) - 1) . '?';
$strTipos = str_repeat('i', count($inputItems));
$stmt = $con->prepare("SELECT id_product, type_product, cover_product, title_product, subtitle_product, price_old, price FROM tbl_products WHERE id_product in ($strMarcas)");
$stmt->bind_param($strTipos, ...$inputItems);
$stmt->execute();
How can I prevent those errors from being generated, when no products are added to the shopping cart?
All your code depends on the number of
$items
in the cart, if there is nothing just don't run the code. You can do a check like this: