codeigniter Insert_batch无效

时间:2017-03-23 05:27:37

标签: php codeigniter batch-insert

我正在尝试使用codeigniter insert_batch将多行插入到我的数据库表中。根据错误报告,似乎没有设置表列。只是数组的数量:

browser error report

我的观点:

<table class="table">
      <center><h3> Activity Name: <?php echo $activity_name; ?></h3><h4> Date: <?php echo $activity_date; ?></h4></center>
      <tr>
          <td>Member Name and Course</td>
          <td>Attendance</td>
          <td>fee</td>
      </tr>
      <?php
      $count = 1;
      $member_count = 1;
      foreach ($students as $row) {
            $uID = $row['user_ID'];
            $cID = $row['council_ID'];
            $aID = $row['activity_ID'];
            $name = $row['user_LastName'].", ".$row['user_FirstName'];
            $course = $row['user_Course'];
            $year = $row['user_Year'];
      ?>
            <tr>
              <td>
                  <h4> <?php echo $member_count.". ".$name; ?></h4>
                  <h5> &nbsp;&nbsp;&nbsp;&nbsp; <?php echo $course." - ".$year; ?></h5>
              </td>
              <?php echo form_open('Isidran/InsertAttendance'); ?>
              <input type="hidden" id="uID<?php echo $count; ?>" name="uID[]" value="<?php echo $uID;?>"> 
              <input type="hidden" id="cID<?php echo $count; ?>" name="cID[]" value="<?php echo $cID;?>"> 
              <input type="hidden" id="aID<?php echo $count; ?>" name="aID[]" value="<?php echo $aID;?>">
              <input type="hidden" id="aname<?php echo $count;?>" name="aname[]" value="<?php echo $activity_name; ?>">
              <td><select id="attendance<?php echo $count; ?>" name="attendance[]" class="form-control">
                    <option value="Absent">  Absent </option>
                    <option value="Present"> Present </option>
                  </select>
              </td>
              <input type="hidden" id="name<?php echo $count;?>" name="name[]" value="<?php echo $name;?>">
              <input type="hidden" id="fines<?php echo $count; ?>" name="fines[]" value="<?php echo $activity_fee; ?>">
              <td><div id="fee<?php echo $count; ?>"><?php echo $activity_fee; ?></div> </td>
            </tr><br />
            <script type="text/javascript">
              $(function() {
                  $('#attendance<?php echo $count; ?>').change(function(){
                    var activity_fee = '<?php echo $activity_fee; ?>'
                      if($('#attendance<?php echo $count; ?>').val() == 'Absent') {
                          $('#fee<?php echo $count; ?>').show();
                          $('#fee<?php echo $count; ?>').text('<?php echo $activity_fee; ?>');
                      } else {
                          $('#fee<?php echo $count; ?>').show(); 
                          $('#fee<?php echo $count; ?>').text('0');  
                          $('#fines<?php echo $count; ?>').val('0'); 
                      } 
                  });
              });
            </script>
        <?php $count++; $member_count++; } ?>
          <tr>
            <td colspan="3"><button type="submit" id="btn_submit" class="btn btn-info pull-right">Submit</button></td>
          </tr>
        <?php echo form_close(); ?> 
      </table>

我的控制器:

public function InsertAttendance()
        {
            $attendance_data = array(
                    'attendance_userID' => $this->input->post('uID[]'),
                    'attendance_councilID' => $this->input->post('cID[]'),
                    'attendance_activityID' => $this->input->post('aID[]'),
                    'attendance_activity' => $this->input->post('aname[]'),
                    'attendance_status' => $this->input->post('attendance[]'),
                    'attendance_sname' => $this->input->post('name[]'),
                    'attendance_fines' => $this->input->post('fines[]'));
            $data['result'] = $this->Site_model->insertAttendance($attendance_data);
            if($data['result'] == true)
                {
                    $this->session->set_flashdata('feedback', '<div class="alert alert-success alert-dismissable">
                                                                        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                                                                            <strong>Added Successfully</strong>
                                                                        </div>');
                    redirect('Isidran/Home');
                }
        }

和型号:

public function insertAttendance($data)
    {
        $this->db->insert_batch('tbl_council_activity_attendance', $data);
        if($this->db->affected_rows() > 0)
            {
                        return true;
            }
    }   

2 个答案:

答案 0 :(得分:0)

尝试

public function insertAttendance($data)
{
    $attendance=array(
      'column1' => $data['index1'],
      'column2' => $data['index2'],
      'column3' => $data['index3'],
      .
      .
    );
    $this->db->insert('tbl_council_activity_attendance', $attendance);
    if($this->db->affected_rows() > 0)
    {
       return true;
    }
}   

答案 1 :(得分:0)

如果要插入批次而不是数据数组

$array_name=array(
                array(
                'col1'=>'data',
                'col2'=>'data',
                'col3'=>'data'
                ),
                array(
                'col1'=>'data',
                'col2'=>'data',
                'col3'=>'data'
                )
);
相关问题