我有一个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在内的所有三个都相同,它会增加数量。
答案 0 :(得分:0)
您可以将此代码放在您的模型上
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
答案 1 :(得分:0)
//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);
}
答案 2 :(得分:0)
在创建数据之前,请检查表
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();
}