簡體   English   中英

Codeigniter:插入多個值,如果已經存在則更新

[英]Codeigniter: INSERT multiple values, UPDATE if already exists

我有一個插入三個外鍵和1個數量的codeigniter查詢。

這是我在模型中的查詢:

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);
}

我想要的是,如果插入的以下三列相同,則student_id,schoolyear_id和feetype_id值存在於同一行中,則不會插入新行,而金額只會添加到現有行的金額中。

請注意:student_id和schoolyear_id可以相同,我想要的是,如果包括feetype_id的所有三個都相同,它將添加金額。

您可以將此代碼放在模型上

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;
    }
}

在您的控制器中,您可以訪問這些功能,因此就像

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);
  }

}

希望這會有所幫助:D

//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);
}

在創建數據之前,請檢查表

    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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM