0

Hello guys I am trying to insert data into my database. Some values contain the same id. I want to sum during insertion and not add a new row:

 if ($q->num_rows() >0) 
 {

        $this->db->where('student_id',$id);

        $this->db->set('mark_obtained', ' mark_obtained+ '. $obtained_marks['mark_obtained'], FALSE);
        $this->db->update('position' , array('mark_obtained' => $obtained_marks , 'student_id' =>$row['student_id'] , 'exam_id'=> $row['exam_id'],'subject_id' =>$row['subject_id'],'section_id'=>$row['section_id'], 'class_id'=>$row['class_id']));
  } 
  else {

        $this->db->insert('position' , array('mark_obtained' => $obtained_marks , 'student_id' =>$row['student_id'] , 'exam_id'=> $row['exam_id'],'subject_id' =>$row['subject_id'],'section_id'=>$row['section_id'], 'class_id'=>$row['class_id']));

  }
BrianC
  • 10,591
  • 2
  • 30
  • 50

1 Answers1

0

Before going to Query you should sum the same id value in array then pass the data into Your Query check this link to

Sum the array having same keys

Boominathan Elango
  • 1,156
  • 2
  • 7
  • 20