Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

php - Add item to the cart and change quentity show following error.how to fix it

Notice: Undefined index: 9781400108503 in C:xampphtdocsibshelf.comcart.php on line 38

Notice: Undefined index: 9781400108503 in C:xampphtdocsibshelf.comcart.php on line 42

Warning: array_count_values(): Can only count STRING and INTEGER values! in C:xampphtdocsibshelf.comcart.php on line 53

// book_isbn got from form post method
if(isset($_POST['bookisbn'])){
    $book_isbn = $_POST['bookisbn'];
}

if(isset($book_isbn))
{
    // new item selected
    if(!isset($_SESSION['cart']))
    {
        // $_SESSION['cart'] is associative array that bookisbn => qty
        $_SESSION['cart'] = array();

        $_SESSION['total_items'] = 0;
        $_SESSION['total_price'] = '0.00';
    }

    if(!isset($_SESSION['cart'][$book_isbn]))
    {
        $_SESSION['cart'][$book_isbn] = 1;
    } elseif(isset($_POST['cart']))
    {
        $_SESSION['cart'][$book_isbn]++;
        unset($_POST);
    }
}

// if save change button is clicked , change the qty of each bookisbn
if(isset($_POST['save_change'])){
    foreach($_SESSION['cart'] as $isbn =>$qty){
        if($_POST[$isbn] == '0')
        {
            unset($_SESSION['cart']["$isbn"]);
        } else {
            $_SESSION['cart']["$isbn"] = $_POST["$isbn"];
        }
    }
}

$title = "Your shopping cart";

if(isset($_SESSION['cart']) && (array_count_values($_SESSION['cart']))){
    $_SESSION['total_price'] = total_price($_SESSION['cart']);
    $_SESSION['total_items'] = total_items($_SESSION['cart']);
question from:https://stackoverflow.com/questions/66058057/add-item-to-the-cart-and-change-quentity-show-following-error-how-to-fix-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

"9781400108503" seems to be the value of $isbn in the for loop on save_change.

On line 42, the notice is triggered for $_POST[$isbn]. Make sure you use isset($_POST[$isbn]). There doesn't seem to be a value posted for this ISBN.


On line 53, you're using array_count_values(). That's not the function you're intending to use. Do count($_SESSION['cart']) instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...