【问题标题】:how to update one table based on another table ID codeigniter如何根据另一个表 ID codeigniter 更新一个表
【发布时间】:2016-01-08 10:33:29
【问题描述】:

我有两个表,第一个表直接与第二个表关联。我使用 codeigniter 和 mysql 作为我的数据库系统。

我的第一个表名为exam,其结构如下图:

这是我的第二张桌子,名为exam_assign_classes

如何根据第一个表 ID 更新第二个表。有时在编辑页面中,用户可以添加分配的批次或删除批次的数量。我尝试计算具有匹配 id 的行数并从第二个表中删除这些行,然后重新插入它,但这似乎很糟糕?

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:
    $this->db->where('`exam_id` IN (SELECT `exam_id` FROM `exam_assign_classes `)', NULL, FALSE);
    $this->db->update('exam', $data); 
    

    【讨论】:

    • 实际上我需要更新第二个表exam_assign_classes,它将有许多与exam_id 关联的行。我已经尝试了您的解决方案,但没有任何反应。我将发布我的模式和控制器代码以更新考试记录。请检查我的代码
    【解决方案2】:

    这是我的代码更新版本,我将一堆数据库任务放在一个更新条件中以更新考试分配表。

    查看:

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

    型号:

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

    从上面的代码我可以实现我真正需要的。但是我在更新考试分配表上的字段时正在删除和插入行。但它显示了我的应用程序性能。那么,更新数据库是一种好习惯吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-18
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 2020-11-04
      相关资源
      最近更新 更多