如何在codeigniter中使用insert_batch插入多个数据

时间:2015-07-28 10:14:37

标签: php codeigniter

这里我将数据插入数据库,但如何在foreach循环中编写这个以获取多个数据请帮助我作为一个更新鲜的我完全糊涂..

我的控制器

class Student extends CI_Controller {

    public function _construct()
    {
        parent::_construct();
        //call model
        $this->load->model("StudentModel","m");
    }

    function index()
    {
        $this->load->view("index");
    }
    function savedata()
    {
        //create array for get data from index
    //$data=array(
                     // 'studentname' => $this->input->post('studentname'),
                      //'gender'  =>  $this->input->post('gender'),
                      //'phone'  =>  $this->input->post('phone')
                  // );

        $data = array(
   array(
      'studentname' => 'Reddy' ,
      'gender' => 'Male' ,
      'phone' => '456879'
   ),
   array(
      'studentname' => 'Yalla' ,
      'gender' => 'Female' ,
      'phone' => '12345678'
   )
);


     //mean that insert into database table name tblstudent

        $this->db->insert_batch('tblstudent',$data);

    //mean that when insert already it will go to page index    
        redirect("Student/index");
    }

    function edit($id)
    {
        $row=$this->m->getonerow($id);
        $data['r']=$row;
        $this->load->view('edit',$data);

    }
    function update($id)
    {
        $id=$this->input->post('id');
        $data=array(
                      'studentname' => $this->input->post('studentname'),
                      'gender'  =>  $this->input->post('gender'),
                      'phone'  =>  $this->input->post('phone')
                   );
                     $this->db->where('id',$id);
                     $this->db->update('tblstudent',$data);
                     redirect("Student/index");

    }
    function delete($id)
    {
        $id=$this->db->where('id',$id);
        $this->db->delete('tblstudent');
        redirect("Student/index");
    }

}

我的模特

class StudentModel extends CI_Model{

    function _construct()
    {
        parent::_construct();
    }
    function gettable()
    {
        $query=$this->db->get('tblstudent');
        return $query->result();
    }
    function getonerow($id)
    {
        $this->db->where('id',$id);
        $query = $this->db->get('tblstudent');
        return $query->row();

    }
}

1 个答案:

答案 0 :(得分:1)

对于CodeIgniter 3.x:insert_batch

对于CodeIgniter 2.x:insert_batch