Php 如何在OpenCart中通过控制器定义MySQL(模型)值

Php 如何在OpenCart中通过控制器定义MySQL(模型)值,php,mysql,opencart,Php,Mysql,Opencart,我试图通过控制器定义MySQL(model.php)值 下面是MySQL的代码 $this->db->query("INSERT INTO " . DB_PREFIX . "cart_to_customer SET customer_id = '" . (int)$this->customer->getId() . "', cart_id = '" . (int)$data['cart_id'] . "'"); 我只想通过controller.php插入cart_id值

我试图通过控制器定义MySQL(model.php)值

下面是MySQL的代码

$this->db->query("INSERT INTO " . DB_PREFIX . "cart_to_customer SET customer_id = '" . (int)$this->customer->getId() . "', cart_id = '" . (int)$data['cart_id'] . "'");
我只想通过controller.php插入cart_id值

比如

$data['cart_id'] = '45';
我用view.tpl的输入尝试了下面的方法,效果很好,但当有空值时,它必须在中插入45,但它给出0

if (isset($this->request->post['cart_id'])) {
            $data['cart_id'] = $this->request->post['cart_id'];
        } else {
            $data['cart_id'] = '45';
        }
如何从控制器中设置值???

如何。。。 假设您拥有控制器文件
catalog/controller/extension/module/your_estension.php
以及与此控制器文件对应的模型文件
catalog/model/extension/module/your_estension.php
。 在控制器文件中:

// you must load model file like this:

    $this->load->model('extension/module/your_estension');

    if (isset($this->request->post['cart_id'])) {
                $data['cart_id'] = $this->request->post['cart_id'];
            } else {
                $data['cart_id'] = '45';
            }
    //next you can send your posted data to the model...
    $this->model_extension_module_your_estension->addCardId($data['cart_id']);

    // next in the corresponding model file you can retrieve that data:

    public function addCardId($card_id) {
    // and write it to DB
    $this->db->query("INSERT INTO " . DB_PREFIX . "cart_to_customer SET customer_id = '" . (int)$this->customer->getId() . "', cart_id = '" . (int)$card_id . "'");
    }

我不明白你的问题$此mysql查询中的数据['cart_id']是通过模型或库请求从控制器设置的。。。例如$this->cart->add($data)。。。卡id位于$data数组中。。请提供更多信息…@OcPh$data['cart_id']在mySQl中是由model设置的,我希望能帮助您。但是你能告诉我更多的信息吗,我们谈论的到底是什么模型和控制器?这将有很大帮助。或者在查询和语句周围添加一些额外的代码、函数?谢谢您的回复。如果我需要传递更多的变量,那么如何传递呢。不仅仅是单个
$data['cart\u id']
。您可以像这样传递它
$this->model\u extension\u module\u your\u estension->addCardId($data['cart\u id'],$second\u variable,$third)
并检索
公共函数addCardId($card\u id,$second,$third){
。或者您可以在数组中传递它。