Codeigniter 购物车无法添加项目

Codeigniter Cart cannot add items

所以我正在尝试创建一个带有购物车的应用程序,当我尝试添加该项目时,它不起作用。顺便说一句,我已经有一个可以工作的购物车应用程序,这就是为什么我想知道为什么它不起作用。我几乎复制了工作中的所有内容。这是代码

手推车控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cart extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->library('cart');
}

public function add_to_cart(){

    $id = $this->uri->segment(3);
    if($this->cart->contents()){
        foreach ($this->cart->contents() as $item){
            if ($item['id']==$id){
                $data = array('rowid'=>$item['rowid'],
                    'qty'=>++$item['qty']);
                $process = $this->cart->update($data);
            }
            else{
                $data = array(
                    'id'=>$id,
                    'qty'=>1,                       
                    'name' => $this->get_data->get_value('product_name','products','product_id', $id),
                    'price' => $this->get_data->get_value('product_price','products','product_id', $id)
                    );
                $process = $this->cart->insert($data);
            }
        }
    }
    else{
        $data = array('id'=>$id,
                    'qty' =>1,
                    'name' => $this->get_data->get_value('product_name','products','product_id', $id),
                    'price' => $this->get_data->get_value('product_price','products','product_id', $id),
                    );
                    $process = $this->cart->insert($data);
    }


    if($process){
        $this->session->set_flashdata('success', 'Successful');
        redirect('products');
    }
    else{
        $this->session->set_flashdata('failed', 'Failed');
        redirect('products');
        //var_dump($process);
    }
}

这是按钮

<div class="button pull-right" style="margin-top: 10px;"><a href="<?php echo base_url().'cart/add_to_cart/'.$row->product_id;?>"><span class="glyphicon glyphicon-shopping-cart"></span>Add to Cart</a></div>

我真的看不出问题所在,我正在使用会话数据库,sess_us_database 已经是 TRUE。我试过使用 var_dump($process) 但它是假的,我试过 var_dump($data) 并且数据似乎很好但插入部分不起作用。有什么想法吗?这对我有很大的帮助,谢谢。

CI 默认购物车只允许 alpha-numeric, dashes, underscores, colons or periods 在产品名称中,如果产品价格是 0 那么它也不会将产品添加到购物车。

请先检查一下。

更改此变量的一个好方法是使用以下代码在 application\libraries\MY_Cart.php 上放置一个 MY_Cart.php:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Cart extends CI_Cart {

    var $product_name_rules = '[:print:]';

}

检查您是否忘记了以下内容:

// $items 数组是否包含 id、数量、价格和名称?这些是必需的

1 - 价格必须是数字并且大于 0。 2 - 名字必须是字母 3 - 数量是数字并且大于 0 。 4 - 必须设置 id

如果您需要添加任何名称,您可以删除此代码:

if ($this->product_name_safe && ! preg_match('/^['.$this-product_name_rules.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $items['name']))
    {
        log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
        return FALSE;
    }

from system/libraries/Cart.php : line 227

当产品加入购物车时替换产品名称中的特殊字符。

示例:EKEN H9R / H9 运动相机超高清 4K / 25fps WiFi 2.0" 170D 水下防水头盔视频录制

// Trim special character from text
    public function trim_special_char($text)
    {
        $str = str_replace("(", '_:', $text);
        $str = str_replace(")", ':_', $str);
        $str = str_replace("/", '_slash', $str);
        $str = str_replace("+", '_plus', $str);
        $str = str_replace("&", '_and', $str);
        $str = str_replace("'", '_ss', $str);
        $str = str_replace("x", '_X', $str);
        $str = str_replace('"', '_cot', $str);

        return $str;
    }

    // Set special character from previous text
    public function set_special_char($text)
    {
        $str = str_replace('_:',  "(", $text);
        $str = str_replace(':_', ")", $str);
        $str = str_replace('_slash', "/", $str);
        $str = str_replace('_plus', "+", $str);
        $str = str_replace('_and', "&", $str);
        $str = str_replace('_ss', "'", $str);
        $str = str_replace('_X', "x", $str);
        $str = str_replace('_cot', '"', $str);

        return $str;
    }