2016-01-27 4 views
3

Я начинаю открывать корзину, и я пытаюсь создать подменю в панели admin, имя файла - item.php, я просто пытаюсь вставить в базу данных (head_text_field, title_text_field и max) & (table is show_product), я стараюсь следовать insert data into database with codeigniter , но все-таки ошибка, ошибка вызова неопределенных метода DB :: вставка() модель \ элемент \ item.phpКак вставить данные в базу данных с помощью opencart?

EDIT ЧАСТЬ 1: когда я удалить этот код в модели:

return $this->db->insert('show_product', $data);

И изменить с помощью этого кода:

$this->db->query("INSERT INTO" . DB_PREFIX . "show_product SET head_text = '" . $this->db->escape($data['head_text_field']) . "', title_text = '" . $this->db->escape($data['title_text_field']) . "', max_item = '" . $this->db->escape($data['max']) . "'"); 

Это работа, но в базе данных все еще пусто ???

Это контроллер в (контроллер/элемент/item.php)

class ControllerItemItem extends Controller { //Controller/Item/Item.php 
private $error = array(); 

public function index() { 
    $this->language->load('item/item'); 

    $this->document->setTitle($this->language->get('heading_title')); 

    $this->load->model('item/item'); 

    $this->getList(); 
} 

protected function getList(){ 

    if (isset($this->request->get['head_text_field'])){ 
     $head_text_field = $this->request->get['head_text_field']; 
    } else { 
     $head_text_field = null; 
    } 

    if (isset($this->request->get['title_text_field'])){ 
     $title_text_field = $this->request->get['title_text_field']; 
    } else { 
     $title_text_field = null; 
    } 

    if (isset($this->request->get['max'])){ 
     $max = $this->request->get['max']; 
    } else { 
     $max = null; 
    } 

    if(isset($this->request->get['product'])){ // product have array in view e.g <input name=product[]> 
     $product = $this->request->get['product']; 
     $products = array(); 
     foreach($product as $p) 
     { 
      $products[] = array($p); 
     } 
    }else { 
     $product = null; 
    } 


    // BREADCRUMBS // 

    $this->data['breadcrumbs'] = array(); 

    $this->data['breadcrumbs'][] = array(
     'text'  => $this->language->get('text_home'), 
     'href'  => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'), 
     'separator' => false 
    ); 

    $this->data['breadcrumbs'][] = array(
     'text'  => $this->language->get('text_module'), 
     'href'  => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'), 
     'separator' => ' :: ' 
    ); 

    $this->data['breadcrumbs'][] = array(
     'text'  => $this->language->get('heading_title'), 
     'href'  => $this->url->link('module/item', 'token=' . $this->session->data['token'], 'SSL'), 
     'separator' => ' :: ' 
    ); 

    // END // 

    // Call Language // 
    $this->data['heading_title'] = $this->language->get('heading_title'); 
    $this->data['entry_head'] = $this->language->get('entry_head'); 
    $this->data['entry_title'] = $this->language->get('entry_title'); 
    $this->data['entry_product'] = $this->language->get('entry_product'); 
    $this->data['entry_max_item'] = $this->language->get('entry_max_item'); 
    $this->data['button_save'] = $this->language->get('button_save'); 
    $this->data['button_cancel'] = $this->language->get('button_cancel'); 

    // END // 

    $this->data['cancel'] = $this->url->link('item/item', 'token=' . $this->session->data['token'], 'SSL'); 
    $this->data['action'] = $this->url->link('item/item/insert', 'token=' . $this->session->data['token'], 'SSL'); 

    $this->template = 'item/item.tpl'; 
    $this->children = array(
     'common/header', 
     'common/footer' 
    ); 

    $this->response->setOutput($this->render()); 

} 

public function insert() 
{ 
    $this->language->load('item/item'); 

    $this->document->setTitle($this->language->get('heading_title')); 

    $this->load->model('item/item'); 

    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 
     $this->model_item_item->insert_head($data); 

     $this->session->data['success'] = $this->language->get('text_success'); 

     $url = ''; 

     $this->redirect($this->url->link('item/item', 'token=' . $this->session->data['token'] . $url, 'SSL')); 
    } 
} 
protected function validateForm() { 
    if (!$this->user->hasPermission('modify', 'catalog/product')) { 
     $this->error['warning'] = $this->language->get('error_permission'); 
    } 

    if ((utf8_strlen($this->request->post['head_text_field']) < 1) || (utf8_strlen($this->request->post['head_text_field']) > 64)) { 
     $this->error['head'] = $this->language->get('error_head'); 
    } 

    if (!$this->request->post['title_text_field']) { 
     $this->error['title'] = $this->language->get('error_title'); 
    } 

    if (!$this->error) { 
     return true; 
    } else { 
     return false; 
    } 
} 
} 

Это для модели (модель \ элемент \ item.php)

class ModelItemItem extends Model { 
public function insert_head() 
{ 
    $head_text_field = $this->get['head_text_field']; 
    $title_text_field = $this->get['title_text_field']; 
    $max = $this->get['max']; 

    $data = array(
     'head_text_field' => $head_text_field, 
     'title_text_field' => $title_text_field, 
     'max'    => $max 
    ); 

    return $this->db->insert('show_product', $data); 
    //$this->db->query("INSERT INTO" . DB_PREFIX . "show_product SET head_text = '" . $this->db->escape($data['head_text_field']) . "', title_text = '" . $this->db->escape($data['title_text_field']) . "', max_item = '" . $this->db->escape($data['max']) . "'"); 

} 
} 

ответ

1

не должен выглядеть ваш запрос soming как:

$this->db->query("INSERT INTO .... "); 

Я не думаю, что OpenCart построен на CodeIgniter, хотя я слышал, что некоторые из его классов SIMUL соток

Попробуйте добавить некоторые пробелы в начале вашего запроса:

$this->db->query("INSERT INTO " . DB_PREFIX . "show_product SET head_text = '" . $this->db->escape($data['head_text_field']) . "', title_text = '" . $this->db->escape($data['title_text_field']) . "', max_item = '" . $this->db->escape($data['max']) . "'"); 
функция

Добавленное() нуждается в

$this->model_item_item->insert_head($data); //wrong 

Переход к:

$this->model_item_item->insert_head($this->request->post); 

И модель должна выглядят примерно так:

public function insert_head($data) 
    { 
    $this->db->query("INSERT INTO " . DB_PREFIX . "show_product SET head_text = '" . $this->db->escape($data['head_text_field']) . "', title_text = '" . $this->db->escape($data['title_text_field']) . "', max_item = '" . $this->db->escape($data['max']) . "'"); 
    } 
+0

Thx для вашего предложения, я уже пробовал так: $ this-> db-> query ("INSERT INTO". DB_PREFIX. "show_product SET head_text = '". $ this-> db-> escape ($ data ['head_text_field']). "', title_text ='". $ this-> db-> escape ($ data ['title_text_field']). "', max_item ='". $ this-> db-> escape ($ data ['max']). "'"); на самом деле это хорошо работает, но в базе данных все еще пусто ???? – Vilthering

+1

Ваши недостающие пробелы в вашем запросе –

+0

О, ваше право в INTO «должно быть INTO», извините, если вы не возражаете, если я снова спрошу, почему значение в базе данных пусто? (успешно введен в базу данных, но пустое значение), я сделал ошибку? – Vilthering

Смежные вопросы