如何使用codeiginter插入数据?

时间:2019-10-30 11:33:57

标签: codeigniter

我是Codeiginter的新手,如何使用Codeiginter将数据插入表中

我的控制器代码;

application(_:url:options:)

2 个答案:

答案 0 :(得分:2)

这是使用互联网进行搜索的简单形式

$data = array(
            'subgrpname' => $this->input->post('subgrpname'),
            'grpname'=> $this->input->post('grpname'),
            'pltype'=> $this->input->post('pltype')
        );  
        $this->db->insert('tablename',$data);

答案 1 :(得分:0)

我想通过遵循MVC编码风格来分享reusable code approach

我的控制器

$table = 'table_name';
$data = array(
  'subgrpname' => $this->input->post('subgrpname'),
  'grpname'=> $this->input->post('grpname'),
  'pltype'=> $this->input->post('pltype')
);
$record_id = $this->Common_Model->insert_into_table($table, $data);

我的Common_Model

function insert_into_table($table, $data) {
    // echo "<pre>";print_r($data);exit;
    $insert = $this->db->insert($table, $data);
    $insert_id = $this->db->insert_id();
    if ($insert) {
        return $insert_id;
    } else {
        return false;
    }
}

您可以根据需要使用尽可能多的时间使用此模型功能。