【问题标题】:PHP help, session error possibily?PHP帮助,会话错误可能?
【发布时间】:2011-04-28 12:50:28
【问题描述】:

我在一个电子商务网站上工作,当用户第一次将产品添加到购物篮时,我收到以下错误,

遇到了 PHP 错误

严重性:通知

消息:未定义索引:orderDetails

文件名:libraries/MY_Cart.php

行号:59 一个 PHP 错误是 遇到

严重性:警告

消息:无法修改标题 信息 - 已发送的标头 (输出开始于 /var/www/vhosts/akulaliving.com/httpdocs/CI-1.7.3/libraries/Exceptions.php:166)

文件名:libraries/Session.php

行号:662

使用以下代码将产品添加到购物篮中,

if ($this->input->post('btnAddToBag'))
        {

            $derivativeId = $this->input->post('selDerivative-1');
            $quantity = $this->input->post('selQuantity');
            $derivative = $this->Product_model->GetProducts(array('pdId' => $derivativeId), 'small');

            // Add item to shopping bag.
            $attributes = $this->Product_model->GetProductDerivatives(array('pdId' => $derivativeId));
            $this->Checkout_model->AddProduct($derivative, $attributes, $quantity);
            $this->data['message'] = 'Item added to Shopping Bag.';

            // Update Delivery Price
            $this->Checkout_model->updateDelivery(49);

            //get the bag details
            $this->data['items'] = $this->Checkout_model->GetProducts();        
        }

被调用的模型函数是这个,

function AddProduct($derivative, $attributes, $quantity)
{
    $data = array(
       'id'         => $derivative->pdId,
       'qty'        => $quantity,
       'price'      => ($derivative->productSavingType == 'none' ? $derivative->productPrice : $derivative->productSavingPrice),
       'name'       => $derivative->productTitle,
       'attributes' => $attributes['attributeValues'],
       'refNo'      => $derivative->pdRefNo,
       'productId'  => $derivative->productId,
       'set'        => $derivative->productIsSet,
       'hasImage'   => $derivative->hasImage,
       'imageUrl'   => $derivative->imageUrl,
       'imageAlt'   => $derivative->imageAlt,
       'stockLevel' => $derivative->pdStockLevel,
       'leadTime'   => $derivative->pdLeadTime
    );

    $data['nonDiscountedPrice'] = $data['price'];
    if ($derivative->productSavingType == 'end-of-season')
    {
        $data['nonDiscountedPrice'] = $derivative->productPrice;
    }

    $this->cart->insert($data);
}

错误抱怨的代码如下,

function _insert($items=array())
{
    if (isset($items['options']) AND count($items['options']) > 0)
    {
        $rowid = md5($items['id'].implode('', $items['options']));
    }
    else
    {
        $rowid = md5($items['id']);
    }

    if (isset($this->_cart_contents[$rowid]))
    {
        if (!isset($items['qty']))
        {
            return FALSE;
        }

        // Already Exists, we need to update the total for this item
        $new_qty = $items['qty'];
        $items['qty'] = $items['qty'] + $this->_cart_contents[$rowid]['qty'];

        $items['rowid'] = $rowid;

        if ($this->_update($items))
        {
            return TRUE;
        }
        return FALSE;
    }

    // Doesn't exist, we need to insert this item.
    if (parent::_insert($items))
    {
        // Update our total.
        if (isset($this->_cart_contents[$rowid]))
        {
            $this->real_total_items += $this->_cart_contents[$rowid]['qty'];
            if ($this->_cart_contents['orderDetails']['discount'] > 0)
            {
                $this->_cart_contents[$rowid]['price'] = $this->_cart_contents[$rowid]['nonDiscountedPrice'];
                $this->_save_cart();
            }
        }
        return TRUE;
    }
    return FALSE;
}

【问题讨论】:

    标签: php session codeigniter cart


    【解决方案1】:

    Joomla?这是一个通知,$this->_cart_contents['orderDetails'] 在使用之前没有定义。您可以预先定义它,或者关闭通知,它应该会消失。

    【讨论】:

      【解决方案2】:

      我敢打赌,您的 PHP 已输出 NOTICE 及其 that 导致会话标头已发送错误。

      在 php.ini 中找到 error_reporting 行并将其更改为该行

      error_reporting = E_ALL & ~E_NOTICE
      

      重新启动您的 apache 实例,看看是否可以解决问题。

      【讨论】:

        【解决方案3】:

        你必须在使用它之前为你的变量定义一个默认值

        解决这个警告。

        【讨论】:

          【解决方案4】:

          您可能已将服务器配置设置为显示 E_NOTICE 的 PHP 错误(这对于开发来说是正常的)。 因为生成了 E_NOTICE (MY_Cart.php),所以首先将其回显到浏览器。 并且因为已经有浏览器输出,这会导致您的警告“无法修改标头信息”(libraries/Session.php)。可能是因为它正在做一个 header() 函数或类似的东西。

          您可以通过修复 E_NOTICE 的原因(忘记对 'orderDetails' 键进行 isset 检查?)或通过在代码开头设置 error_reporting(E_ALL & ~E_NOTICE) 来隐藏 E_NOTICE 来解决此问题。

          【讨论】:

            【解决方案5】:
            if (**$this->_cart_contents['orderDetails']['discount'] > 0**)
                    {
                        $this->_cart_contents[$rowid]['price'] = $this->_cart_contents[$rowid]['nonDiscountedPrice'];
                        $this->_save_cart();
                    }
            

            CI 抛出通知消息,因为 ::_cart_contents['orderDetails'] 变量不存在。

            如果您不想更改 php.ini 的错误设置,可以如下更改并尝试:

            if (isset($this->_cart_contents['orderDetails']['discount'])&& ($this->_cart_contents['orderDetails']['discount']>0))
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-05-31
              • 2011-07-15
              • 1970-01-01
              • 2016-05-31
              相关资源
              最近更新 更多