php – Codeigniter – duplicated results after update-ThrowExceptions

Exception or error:

I have a page for adding scores of students in a school. After submitting scores of the students, the input boxes are repopulated with the results from the database.

This was done so that

  1. Teachers who are unable to complete adding scores for students can come back and continue from where they left off.

  2. If an edit is to be made, teachers will know where and what to be edited.

  3. And generally for revision purposes.

However, I noticed that when an edit is made for a particular student, that subject(s) is duplicated for everyone in the class even if they have their scores already.

Please see image below to get an idea of what I mean.

duplicated result

MY MODEL:

public function addrn($data3) {
    $this->db->insert_batch('mtscores_rn', $data3);
    return $this->db->affected_rows(); 
}

MY CONTROLLER:

function assigngradeActionRNMT() 
{   
//var_dump(count($this->input->post('number')));         

for($i=0; $i<count($this->input->post('number')); $i++)
    {    
$data3[]=array(
'section_id' => $this->input->post('section_id'),
'subject_id' => $this->input->post('subject_id'),
'class_id' => $this->input->post('class_id')[$i],
'student_id' => $this->input->post('student_id')[$i],
'session_id' => $this->input->post('session_id'),
'mt_ca1' => $this->input->post('mt_ca1')[$i],
'mt_ca2' => $this->input->post('mt_ca2')[$i],
'mt_ca3' => $this->input->post('mt_ca3')[$i],
);
}

//var_dump($data3);

$inserted = $this->mtprimary_model->addrn($data3);
if($inserted > 0)
    {
$this->session->set_flashdata('msg', '<div class="alert alert-success">Grade Added successfully</div>');
//Echo back success json
redirect('admin/mtprimary/index');
    }
}

MY VIEW:

<?php foreach($students as $student){ ?>
<div class="row">
<div class="col-lg-3">
<div class="form-group">
<label>Student Name</label>
<input type="hidden" name="number[]"  value="">
<input type="hidden" name="section_id"  value="<?php echo $section_id; ?>">
<input type="hidden" name="session_id"  value="<?php echo $student->session_id; ?>">
<input type="hidden" name="student_id[]"  value="<?php echo $student->student_id; ?>">
<input type="hidden" name="class_id[]" value="<?php echo $class_id; ?>">
<input type="text"  value="<?php echo $CI->GetStudentNameWithID($student->student_id); ?>" class="form-control "  readonly>
</div>
</div>

<div class="col-lg-3">
<label>Class Expectation </label>
<textarea name="mt_ca1[]"  class="form-control"  rows="3"  ><?php echo $student->mtscores? $student->mtscores->mt_ca1: 0; ?></textarea>
</div>

<div class="col-lg-3">
<label>Milestone Achieved</label>
<textarea name="mt_ca2[]"  class="form-control"  rows="3"  ><?php echo $student->mtscores? $student->mtscores->mt_ca2: 0; ?></textarea>
</div>

<div class="col-lg-2">
<div class="form-group">
<label>Remark</label>
<input type="text" name="mt_ca3[]"  class="form-control" value="<?php echo $student->mtscores? $student->mtscores->mt_ca3: 0; ?>" >
</div>
</div>                                           
</div>
How to solve:

Use this Updated Code in Your Model


 public function addrn($data3)
    { 
       $statusIns=$this->checkAlreadyExistOrNor();
       if($statusIns==0){
        $this->db->insert_batch('mtscores_rn', $data3);
       }else{
         $this->db->where($data3);
         $this->db->update('mtscores_rn', $data3);
       } 
      return $this->db->affected_rows(); 
    }

public function checkAlreadyExistOrNor($data3){
    $this->db->select(*);
    $this->db->where($data3);
    $result=$this->db->get();
    $listData=$result->result_array();
    if(!empty($listData)){
      return 1;
    }else{
     return 0;
   }

}

Leave a Reply

Your email address will not be published. Required fields are marked *