简体   繁体   English

如何基于另一个表ID Codeigniter更新一个表

[英]how to update one table based on another table ID codeigniter

I have two tables, first table is directly associated with the second table. 我有两个表,第一个表直接与第二个表关联。 I'm using codeigniter and mysql as my database system. 我正在使用codeigniter和mysql作为数据库系统。

My first table called exam and its structure on below picture: 我的第一个表格称为exam及其结构,如下图:

Exam_tbl

This is my second table called exam_assign_classes : 这是我的第二张表,名为exam_assign_classes

Exam_Assign_Tbl

How do I update the second table based on first table id. 如何根据第一个表ID更新第二个表。 Sometimes in the edit page a user can add assigned batch or remove number of batch. 有时,用户可以在编辑页面中添加分配的批次或删除批次数量。 I have tried counting numbers of rows with matching ids and delete those from second table and then re-insert it but it seems so bad practice? 我试过对具有匹配ID的行数进行计数,然后从第二个表中删除它们,然后重新插入它,但这似乎是一种不好的做法?

$this->db->where('`exam_id` IN (SELECT `exam_id` FROM `exam_assign_classes `)', NULL, FALSE);
$this->db->update('exam', $data); 

and this is my updated version of code where i placed bunch of database task in one update condition to update the exam assign table. 这是我的更新版本的代码,在其中我将一堆数据库任务置于一个更新条件下,以更新考试分配表。

View: 视图:

 <div class="form-group col-xs-12 col-sm-6 col-md-4"> <span class="input-group-addon"><i class="fa fa-asterisk" aria-hidden="true"></i> <i class="fa fa-calendar" aria-hidden="true"></i> <?php echo get_phrase('select_batchs'); ?></span> <select class="form-control" name="assigned_batch[]" data-plugin="select2" multiple> <?php foreach($program_list as $program): $batch_list = $this->crud_model->get_batchs_by_program_id($program['program_id']); ?> <optgroup label="<?php echo $program['name']; ?>"> <?php foreach($batch_list as $batch): ?> <option value="<?php echo $batch['batch_id']; ?>" <?php foreach($assigned_batchs as $assignedRow){ if($assignedRow['batch_id'] == $batch['batch_id']) echo 'selected';} ?>><?php echo $batch['batch_name']; ?></option> <?php endforeach; ?> </optgroup> <?php endforeach; ?> </select> </div> 

model: 模型:

 public function delete_associated_exam_assign($exam_id = '') { $this->db->where('exam_id', $exam_id); $this->db->delete('exam_assign_classes'); return true; } public function insert_associated_exam_assign($data2){ $this->db->insert('exam_assign_classes', $data2); return true; } 

 if($param1 == 'do_update'){ $data['name'] = $this->input->post('exam_title'); $data['start_date'] = $this->input->post('start'); $data['end_date'] = $this->input->post('end'); $data['status'] = $this->input->post('status'); if(isset($_POST['is_entrance'])): $data['entrance_exam'] = $this->input->post('is_entrance'); endif; $exam_id = $param2; // update exam table $this->crud_model->update_exam_details($exam_id, $data); // update exam assign table /*First of all delete all those associated rows with matching ids*/ $this->crud_model->delete_associated_exam_assign($exam_id); /*insert newly assign classes*/ foreach($this->input->post('assigned_batch[]') as $batchs){ $data2['exam_id'] = $exam_id; $data2['batch_id'] = $batchs; $this->crud_model->insert_associated_exam_assign($data2); } $this->session->set_flashdata('flash_message' , get_phrase('data_updated')); redirect(base_url() . 'index.php?admin/exam_list/', 'refresh'); } 

From above code i can achieve what i really need. 通过以上代码,我可以实现我真正需要的功能。 But i am doing deleting and insert rows while updating fields on exam assign table. 但是我在更新考试分配表上的字段时正在删除和插入行。 But it show down my app performance. 但这会降低我的应用程序性能。 so, is it a good practice or not to update the database ?? 所以,更新数据库是个好习惯吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM