如何使用codeigniter插入和更新多个选定值?

blpfk2vs  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(330)

所以我对多对多的关系有问题。目前,我有 surat 以及 surat_user table。如何将数据插入到 surat 同时从中插入多个值 select2 多种形式 surat_user 表以及如何获取数据以便我可以更新它。
更新:我解决了插入问题,请看下面的答案
但现在我不知道如何更新这些值。例如 surat_user ```
id_surat | id_user
1 | 1
1 | 2

如何更新 `surat_user` (在控制器和模型中)如果要删除 `id_user` 其中“id\u surat=1”
目前,我不知道如何将多个值提取到select2 form edit中,因此下面是mu uncomplete代码:
控制器

public function edit_sm($id_surat){
$this->load->view('template/header');
$this->load->view('template/sidebar');
$where = array('id_surat' => $id_surat);
//$data=array('id_status'=> $this->M_sm->get_option());
$data['surat'] = $this->M_sm->edit_sm($where,'surat')->result();
$this->load->view('v_edit_surat',$data);
$this->load->view('template/footer');
}

public function edit_sm_proses() {
$data = array(
'id_surat'=>$this->input->post('id_surat'),
'no_surat'=>$this->input->post('no_surat'),
'id_status'=>$this->input->post('id_status'),
'id_user'=>$this->input->post('id_user')
);
$where = array(
'id_surat' => $id_surat
);

$this->M_sm->edit_sm_proses($where,$data,'surat');
redirect('SuratMasuk');

}

型号:

public function edit_sm($where,$table){
$this->db->join('status_surat', 'status_surat.id_status = surat.id_status');
return $this->db->get_where($table,$where);
}

public function edit_sm_proses($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}

看法
    <div class="form-group">
      <label class="control-label col-lg-2">No Surat</label>
      <div class="col-lg-5">
        <input type="text" name="no_surat" class="form-control no_surat" placeholder="Masukkan Nomor Surat" value="<?=$key->no_surat?>">
        <span class="help-block"></span>
      </div>
    </div>
    <br>
    <br>
    <div class="form-body">
    <div class="form-group">
      <label class="control-label col-lg-2">Status</label>
      <div class="col-lg-5">
        <select class="form-control select2 id_status" name="id_status" style="width: 100%;">
            <option value="<?=$key->id_status;?>" selected="<?=$key->id_status;?>"><?php echo $key->status;?></option>
            <?php foreach ($id_status as $row) { ?>
            <option value="<?php echo $row->id_status; ?>"> <?php echo $row->status; ?></option>

            <?php } ?>
            </select>
       <span class="help-block"></span>
      </div>
    </div>
  </div>
  <br>
  <br>
  <div class="form-body">
    <div class="form-group">
      <label class="control-label col-lg-2">Disposisi</label>
      <div class="col-lg-5">
        <select class="form-control select2 id_user" name="id_user[]" style="width: 100%;">
            <option value="<?=$key->id_user;?>" selected="<?=$key->id_user;?>"><?php echo $key->nama;?></option>
            <?php foreach ($id_user as $row) { ?>
            <option value="<?php echo $row->id_user; ?>"> <?php echo $row->nama; ?></option>

            <?php } ?>
            </select>
       <span class="help-block"></span>
      </div>
    </div>
  </div>  
      </div>
      <!-- /.box-body -->
      <div class="box-footer">
       <button class="btn btn-danger" type="reset" value="reset">Reset</button>
       <button class="btn btn-info">Update</button><br>
       <a href="<?php echo base_url(); ?>SuratMasuk/">Kembali</a>
      </form>
       <?php  
            }
            ?>
      </div>
      <!-- box-footer -->
      </div>
      <!-- /.box -->
</div>
<!-- /.col -->
of1yzvn4

of1yzvn41#

首先,显示数据库中的所有记录,并为每个记录添加一个按钮来编辑记录。
note:- don不使用 <form> 圈内

<a class="btn btn-warning btn-sm" href="<?= base_url('SuratMasuk/edit_sm/'.$row->id_surat)?>"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit</a>

编辑控制器
我只是在这里添加后端代码
其中and table是db的保留字。最好不要使用它或以其他方式使用。

public function edit_sm($id_surat){ 
    $condition_array = array('id_surat' => $id_surat);
    $data['surat'] = $this->M_sm->edit_sm($condition_array ,'surat');
}

编辑模型
获取要编辑的记录
此函数仅从数据库获取一条记录,因此请使用 row() ```
public function edit_sm($condition_array ,$record_in){
$this->db->join('status_surat', 'status_surat.id_status = surat.id_status');
return $this->db->get_where($record_in, $condition_array )->row();
}

现在,将显示的表单中填充了数据。
从控制器获取更新数据

public function edit_sm_proses() {
$data = array(
'id_surat'=> $this->input->post('id_surat'),
'no_surat'=>$this->input->post('no_surat'),
'id_status'=>$this->input->post('id_status'),
'id_user'=>$this->input->post('id_user')
);
$condition_array = array(
'id_surat' => $this->input->post('id_surat')
);

$this->M_sm->edit_sm_proses($condition_array, $data, 'surat');
redirect('SuratMasuk');

}

更新模型中的数据

public function edit_sm_proses($condition_array, $data, $record_in){
$this->db->where($condition_array);
return $this->db->update($record_in, $data);
}

tv6aics1

tv6aics12#

class SuratMasuk extends CI_Controller {
    public function add_sm_proses(){
    $data = array(
                'no_surat'=>$this->input->post('no_surat'),
                'id_status'=>$this->input->post('id_status'),   
        );

像这样从表中选择max id

$id_surat = $this->db->query('SELECT MAX(surat.id_surat) AS maxid FROM surat')->row()->maxid;

update:- $id_surat 将给出表中存在的最大id。不用于下一个记录。首先,将它的值增加1,然后使用它

$id_surat = $id_surat + 1;
$id_user = $this->input->post('id_user') ? $this->input->post('id_user') : array();

首先,检查 $id_user 否则foreach循环将给出e错误

if(count($id_user) > 0){
        foreach ($id_user as $u){
            $data_values = array('id_surat' => $id_surat, 'id_user' => $u);
            $this->db->insert('surat_user', $data_values);
        }
    }
    $insert = $this->M_sm->add_sm($data);
``` `$insert` 不会总是回来 `1` . 所以,只检查值是否存在

if($insert){
redirect('SuratMasuk');
} else {
echo "Gagal menambahkan data";

    }
}

}

zi8p0yeb

zi8p0yeb3#

设法自己解决这个问题。
我将控制器更改为:

public function add_sm_proses(){
    $data = array(
        'no_surat'=>$this->input->post('no_surat'),
        'id_status'=>$this->input->post('id_status')    
    );
    $insert = $this->M_sm->trans_surat_user($data);
    if($insert=1){
        redirect('SuratMasuk');
    }   else {
        echo "<h2>Gagal menambahkan data</h2>";

    }
}

将我的模型更改为:

public function trans_surat_user($data){
    $this->db->trans_start();
    $this->db->insert('surat', $data);
    $id_surat = $this->db->query('SELECT surat.id_surat FROM surat ORDER BY id_surat DESC limit 1');
    foreach ($id_surat->result() as $row) {
        $id_surat_result = $row->id_surat;
    }
    $id_user = $_POST['id_user'];
    foreach ($id_user as $data2) {
        array_push($id_user, $data2);
        $this->db->query('INSERT INTO surat_user (id_surat,id_user) VALUES ('.$id_surat_result.','.$data2.')');

    }
    $this->db->trans_complete();
}

相关问题