微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – CodeIgniter批量插入特定列

我有一个CodeIgniter应用程序和一个具有以下结构的 MySQL表:
Table shortlisted_candidates
id int PRIMARY KEY AUTO INCREMENT,candidate_no int NOT NULL,written_marks int,viva_marks int

我想在这个表中执行insert_batch,但是数据只会插入id和candidate_no列中.

我知道Codeigniter Active Records Class为批量插入提供了$this-> db-> insert_batch()函数,但它实际上是在整个表中插入数据,而我希望数据只插入到特定的列中.我怎样才能在CodeIgniter中实现这一目标?

请注意,id是AUTO INCREMENT,PRIMARY KEY列.

我的控制器代码

class Shortlisted_candidates extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('Shortlisted_candidate');
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->helper('form');
        $this->output->enable_profiler(false);
    }
    function add(){
        $data = array();
        if ($_POST) {

            $data['candidate_no'] = $this->input->post('candidate_no');
            $this->Shortlisted_candidate->add_candidate_to_shortlist($data);
            $this->session->set_flashdata('message','Data Successfully Added');
            redirect('shortlisted_candidates/add');
        }
    }
}

我的型号代码

class Shortlisted_candidate extends CI_Model
{

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function add_candidate_to_shortlist($data){
        //Following code inserts data in ALL COLUMNS
        $this->db->insert_batch('shortlisted_candidates',$data);
        //How to write active record batch insert query for inserting data in only `id` and `candidate_no` column?
    }
}
你需要修改以下控制器功能.
function add(){
    $data = array();
    if ($_POST) {

        $data[0]['candidate_no'] = $this->input->post('candidate_no');
        $this->Shortlisted_candidate->add_candidate_to_shortlist($data);
        $this->session->set_flashdata('message','Data Successfully Added');
        redirect('shortlisted_candidates/add');
    }
  }

除此之外,您需要修改表模式,以便设置其他字段的认值.

原文地址:https://www.jb51.cc/php/137369.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐