php - How insert data to database using opencart? -
i begginer open cart, , try build sub menu in panel admin, file name item.php, trying insert database (head_text_field,title_text_field , max) & (table show_product), try follow insert data database codeigniter, still error, error call undefined method db::insert() model\item\item.php
edit part 1: when remove code in model:
return $this->db->insert('show_product', $data);
and change code :
$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']) . "'");
it's work in database still empty ???
this controller in (controller/item/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 $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; } } }
this model (model\item\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']) . "'"); } }
shouldn't query soming :
$this->db->query("insert .... ");
i don't think opencart built on codeigniter although i've heard of classes simular
try adding spaces start of query:
$this->db->query("insert " . 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']) . "'");
your insert() function needs
$this->model_item_item->insert_head($data); //wrong
changing :
$this->model_item_item->insert_head($this->request->post);
and model should like:
public function insert_head($data) { $this->db->query("insert " . 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']) . "'"); }
Comments
Post a Comment