无法使用数据数组中的变量(仅字符串)将数据添加到购物车


Cannot add data to cart with variables in data array (only strings)

我有一个产品页面,当用户单击"添加到购物车"按钮时,将触发下面的函数,并将desk_id和price解析到该函数中。然后,变量被引用到插入到购物车中的购物车数据数组。

我的问题是,$data数组中的这些变量不会将任何内容添加到购物车中,但当我只添加字符串时,它可以正常工作。我已经进行了测试,以确保数据库正确返回产品标题,并尝试将这些变量转换为字符串,但没有什么不同。

    function request_desk_booking($desk_id, $price){
    $this->load->model('search_model');
    //Query database to get desk title because parsing strings through the url does not work
    if($query = $this->search_model->desk_details_cart($desk_id)){
        //convert title to string
        $title =  $query['company'].', '.$query['Town'];
        $name =  (string) $title;
        //check to make sure values are not null
        if($desk_id !== null && $price !== null){
            $this->load->library('cart');
            $data = array(
                   'id'      => $desk_id,
                   'qty'     => 1,
                   'price'   => $price,
                   'name'    => $name
            );
            //insert data into cart
            $this->cart->insert($data);
            $this->load->view('request_booking', $data);
        }else{
            $this->load->view('request_booking');
        }
    }
}

Cart库对提供预先验证的数据非常敏感。确保$price变量没有美元符号,并且是is_numeric()。数量很好,因为你很难编码为1。idname也是必需的,所以它们也可能导致它静默地失败。

我会将日志记录增加到4,并让它告诉你哪里出了问题(它会这样做的!)。

$title上的","导致了此问题。当我删除它时,所有问题都解决了。此外,购物车内容必须始终有一个id、数量、价格和名称。删除其中任何一个,它将不会将项目添加到购物车中。