在 Opencart 中使用自定义视图插入数据

Insert Data Using Custom View In Opencart

当用户提交数据时我会得到这个错误

致命错误:未捕获错误:在 C:\xampp\htdocs\smac\catalog\controller\payment\payment.php:45[=13 中调用 null 上的成员函数 insert() =]

这是我的 tpl 文件

<script type="text/javascript">
    $('#submit').submit(function() {
    $.ajax({ // create an AJAX call...
        data: $(this).serialize(), 
        type: $(this).attr('method'),
        url: $(this).attr('action'), 
        success: function(response) { 
            $('#submit').html(response); 
        }
    });
    return false; 
});
</script>
    <form action="<?php echo $action; ?>" method="post">
    Your name: <input type="text" name="name" required><br>
    Your e-mail: <input type="text" name="email" required><br>
    <input type="submit" value="submit" name="submit">
    </form

控制器文件:

public function index() 
    { 
    $this->load->model('payment/payment');
    $this->data['action'] = $this->url->link('payment/payment');

if (($this->request->server['REQUEST_METHOD'] == 'POST')) {

    $id= $this->model_payment_payment->insert($this->request->post);
        }

        $this->response->setOutput($this->load->view('payment/payment', $data));    
    }

这是我的模型文件

<?php  
class ModelPaymentPayment extends Model 
{  
    public function insert($data) 
    {

        $this->db->query("INSERT INTO " . DB_PREFIX . "naveen SET 
                name = '" . $this->db->escape($data['name']) . "', 
                email = '" . $this->db->escape($data['email']) . "'");

    $address_id = $this->db->getLastId();
        return $address_id;
    }
}

如有错误请指出

我认为问题出在您的控制器文件中。您应该使用

$this->data['action'] = $this->url->link('payment/payment');
$this->response->setOutput($this->load->view('payment/payment', $this->data))

$data['action'] = $this->url->link('payment/payment');
$this->response->setOutput($this->load->view('payment/payment', $data))