簡體   English   中英

PHP購物車

[英]PHP Shopping Cart

我正在嘗試將產品添加到我的購物車中。

我收到一個錯誤說:

警告:為foreach()提供的參數無效

它告訴我,我收到以下代碼的錯誤:

function isInCart($id) {
if (!empty($_SESSION['sess_uid']['cart'])) {
    foreach ($_SESSION['sess_uid']['cart'] as $report) {
        if ($report['reportID'] == $id) {
            // Report ID found in Cart
            return true;
        }
    }
    // Looped through cart, ID not found
    return false;
} else {
    // Cart empty
    return false;
}
}

上面標記錯誤的特定行是:

foreach ($_SESSION['sess_uid']['cart'] as $report) {

我還收到以下錯誤消息:

致命錯誤:只能通過引用傳遞變量

與此相關的代碼如下:

function addToCart($id) {
$report = getReportByID($id);
$author = $report['userID'];

if (!empty($report)) {
    // Got the report
    if (!empty($_SESSION['sess_uid']['cart'])) {
        if (!isInCart($id) && !isOwner($author) && !hasPurchased($id)) {
            array_push($_SESSION['sess_uid']['cart'], $report);
            return true;
        } else {
            return false;
        }
    } else {
        $_SESSION['sess_uid']['cart'] = array();

        if (!isInCart($id) && !isOwner($author) && !hasPurchased($id)) {
            array_push($_SESSION['sess_uid']['cart'], $report);
            return true;
        } else {
            return false;
        }
    }
} else {
    // Unable to get report by ID
    return false;
}
}

上面標記錯誤的特定代碼行是:

array_push($_SESSION['sess_uid']['cart'], $report);

下面的代碼是我的報告填充商店的原因

<?php

function getReportByID($id) {
$conn = new mysqli(localhost, root, DBPASS, DBNAME);
$sql = "SELECT * FROM reports WHERE reportID = '" . $conn->real_escape_string($id)."';";
// Performs the $sql query on the server
$report = $conn->query($sql);

return $report->fetch_array(MYSQLI_ASSOC);
}

?>

任何幫助將不勝感激。

謝謝

我認為這將有所幫助:它將您的會話打印為一個數組,所以即使會話是空的,您也不會收到錯誤

foreach ((array)$_SESSION['sess_uid']['cart'] as $report) {

如果這樣可以解決錯誤,請告訴我?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM