1

I have a codeigniter query that Insert three foreign keys and 1 amount.

This is my query in the model:

public function create()
{
  $insert_data = array(
    'student_id' => $this->input->post('feestudentstudent'),
    'schoolyear_id' => $this->input->post('feestudentschoolyear'),
    'feetype_id' => $this->input->post('feestudentfeetype'),
    'feestudent_amount' => $this->input->post('feestudentamount')
  );
  $status = $this->db->insert('tbl_feestudent', $insert_data);
}

What I want is if the following three columns inserted is the same, the student_id, schoolyear_id and feetype_id values exists in the same row it will not insert a new row and the amount will just add to the amount of the existing row.

Take Note: It's okay that student_id, and schoolyear_id is the same, what I want is if all the three including feetype_id is the same it will add the amount.

6
  • 1
    You should use ON DUPLICATE KEY UPDATE dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html - just write your own sql query. Commented Jul 9, 2017 at 12:07
  • Does this apply on same foreign keys? or is it applicable only in same Primary keys? Commented Jul 9, 2017 at 12:08
  • "f you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY" - so Primary or unique foreign key Commented Jul 9, 2017 at 12:10
  • But what if for example, I want to insert an amount in the same schoolyear_id with same student_id but it only checks if the feetype_id exists is it possible? Commented Jul 9, 2017 at 12:13
  • You should create unique index on (student_id, schoolyear_id and feetype_id). On insert when mysql get error on duplicate key - it just update non unique keys values. Commented Jul 9, 2017 at 12:18

3 Answers 3

1
//MODEL    
public function upinsert($tabel,$data){
        $update='';
        $separator='';
        foreach ($data as $key => $value) {
          $update.=$separator." `$key` = '$value' ";
          $separator=',';
        }
        $sql = $this->db->insert_string($tabel, $data) . ' ON DUPLICATE KEY UPDATE '.$update;
        $this->db->query($sql);
        return $this->db->insert_id();
 }



//Your controller
public function create(){
  $insert_data = array(
    'student_id' => $this->input->post('feestudentstudent'),
    'schoolyear_id' => $this->input->post('feestudentschoolyear'),
    'feetype_id' => $this->input->post('feestudentfeetype'),
    'feestudent_amount' => $this->input->post('feestudentamount')
  );
  $status = $this->_model_name_here->upinsert('tbl_feestudent', $insert_data);
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can put this code on your model

public function isExists($key,$valkey,$tabel)
{
    $this->db->from($tabel);
    $this->db->where($key,$valkey);
    $num = $this->db->count_all_results();
    if($num == 0)
    {
        return FALSE;
    }else{
        return TRUE;
    }
}

and in your controller,you can access those function,so it would be like

public function create()
{
   $student_id = $this->input->post('feestudentstudent');

   $insert_data = array(
  'student_id' => $this->input->post('feestudentstudent'),
  'schoolyear_id' => $this->input->post('feestudentschoolyear'),
  'feetype_id' => $this->input->post('feestudentfeetype'),
  'feestudent_amount' => $this->input->post('feestudentamount')
  );

  $update_data = array(
  'schoolyear_id' => $this->input->post('feestudentschoolyear'),
  'feetype_id' => $this->input->post('feestudentfeetype'),
  'feestudent_amount' => $this->input->post('feestudentamount')
  );

  //call the function
  $check = $this->(your model)->isExists('student_id',$student_id,'tbl_feestudent');

  if($check)
  {
    $this->db->where('student_id',$student_id);
    $this->db->update('tbl_feestudent', $update_data); 
  }
  else
  {
    $this->db->insert('tbl_feestudent', $insert_data);
  }

}

Hope this helps :D

1 Comment

Thanks for the idea, I will try this :)
0

before create data you check the table

    public function create()
    {
    $checkresult = $this->checkexist('tbl_feestudent',array('student_id'=>$this->input->post('feestudentstudent'),'schoolyear_id'=>$this->input->post('feestudentschoolyear'),'feetype_id'=>$this->input->post('feestudentfeetype')));
      $insert_data = array(
        'student_id' => $this->input->post('feestudentstudent'),
        'schoolyear_id' => $this->input->post('feestudentschoolyear'),
        'feetype_id' => $this->input->post('feestudentfeetype'),
        'feestudent_amount' => $this->input->post('feestudentamount')
      );
if($checkresult){
$this->db->where('student_id',$this->input->post('feestudentstudent'));
      $status = $this->db->update('tbl_feestudent', $insert_data);
    }else{
      $status = $this->db->insert('tbl_feestudent', $insert_data);
}

    public function checkexist($table,$where){
$this->db->select('*');
$this->db->select($table);
$this->db->where($where);
return $this->db->get()->result_array();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.