简体   繁体   English

在数据库Codeigniter中插入多个值?

[英]Inserting multiple values in database codeigniter?

I want to insert multiple rows in the database on one click if row is checked using multiple checkbox 如果要使用多个复选框选中行,我想单击一下即可在数据库中插入多行 在此处输入图片说明

Here is my code=> 1)Controller: guard.php (Here I take a array of list of student id's and pass them one by one to the another function get_leave_data which take id of a student and return more information about the student from another table leave_application ). 这是我的代码=> 1)控制器: guard.php (在这里,我获取了一组学生ID列表,并将它们一个一个地传递给另一个函数get_leave_data,该函数获取一个学生的ID,并从另一个返回有关该学生的更多信息表leave_application )。

public function students_out(){
    $request=$this->input->post('approve_leave_status');
    if($request=="OUT"){
    $check_list_out[]=$_POST['check_list_out'];
    if(!empty($check_list_out)){
          foreach ($check_list_out as $check_list_id) {
            $student_data=$this->get_leave_data($check_list_id);
            $this->insert_student_outside($student_data);
          }
          $this->load->helper('url');
          $this->load->view('view_guard');
    }
}else{
     $this->load->helper('url');
     $this->load->view('view_guard');
}

} `

 public function get_leave_data($id){
 $this->load->model('model_guard');
 $data=$this->model_guard->get_data($id);
 return $data;
}

public function insert_student_outside($std_data){
  $this->load->model('model_guard');
  $data=$this->model_guard->insert_student_out($std_data);
 return $data;
}

2)Model: model_guard.php (The functions get_data() and get_data2() return more informations about student and function insert_student_out() insert the student to the student_outside table ) 2)模型:model_guard.php(函数get_data()get_data2()返回有关学生的更多信息,函数insert_student_out()将学生插入到student_outside table

public function get_data($id){
    $this->db->select('leave_id,leave_from_roll_no,leave_student_name,leave_going_to,leave_from_date,leave_till_date,leave_hostel_no,leave_status');
    $this->db->from('leave_application');
    $this->db->where('leave_id',$id[0]);
    $query=$this->db->get();
    $data1=$query->result();
    $data2=$this->get_data2($data1);
    $final_array=array_merge($data1,$data2);
    return $final_array;
    }

    public function get_data2($array){
    foreach ($array as $key) {
    $roll_no=$key->leave_from_roll_no;
    }
    $this->db->select('student_year,student_semester,student_parent_email');
    $this->db->from('students');
    $this->db->where('student_roll_no',$roll_no);
    $query=$this->db->get();
    $data=$query->result();
    return $data;
    }

    public function insert_student_out($std_data){
    $roll_no=$std_data[0]->leave_from_roll_no;
    $id=$std_data[0]->leave_id;
    $date_out=date('Y-m-d');
    $inside_date=NULL;
    $date_allowed=$std_data[0]->leave_till_date;
    $array=array(
    'outside_id'=>$id,
    'outside_roll_no'=>$roll_no,
    'outside_date_out'=>$date_out,
    'outside_date_in'=>$inside_date,
    'outside_date_allowed'=>$date_allowed
    );
    if($this->db->insert('students_outside',$array)){
        return true;
    }else{
        false;
    }


    }

you are trying to insert batch_array using codeignator 您正在尝试使用codeignator插入batch_array

the right syntax to using batch_array is:- 使用batch_array的正确语法是:

$array=array(
    'coloum_name'=>$id,
    'coloum_name'=>$roll_no,
    'coloum_name'=>$date_out,
    'coloum_name'=>$inside_date,
    'coloum_name'=>$date_allowed
    );

if($this->db->insert_batch('students_outside',$array)){
        return true;
    }else{
        false;
    }

you can read manual insert_batch here 您可以在此处阅读手册insert_batch

ok, i am assuming that in $std_data you have more than one record.then in your model's insert_student_out() function try this- 好的,我假设在$std_data您有多个记录。然后在模型的insert_student_out()函数中尝试以下操作-

public function insert_student_out($std_data){

 foreach($std_data as $row){
       $roll_no=$row->leave_from_roll_no;
       $id=$row->leave_id;
       $date_out=date('Y-m-d');
       $inside_date=NULL;
       $date_allowed=$row->leave_till_date;
       $array=array(
              'outside_id'=>$id,
              'outside_roll_no'=>$roll_no,
              'outside_date_out'=>$date_out,
              'outside_date_in'=>$inside_date,
              'outside_date_allowed'=>$date_allowed);

       $this->db->insert('students_outside',$array);

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM