如何在Codeigniter中按ID更新3个表的现有数据

时间:2019-06-12 06:52:34

标签: php mysql codeigniter

我有数据库服务器中已经存在的数据,我想根据id表进行更改。我想一次更新,以便3个数据表也更新,因为表ID也在另一个表中。我尝试过,但不能。请帮助我

我已经从几张桌子中加入了。 我还搜索了:link! :link

这是我的控制器

public function updateservicenew(){
$id= $this->uri->segment(3);
$data['vendor']= $this->model_vendor->show_data_all();
$data['vendor2']= $this->model_vendor->show_data_vendor2();
$data['service']= $this->Madmin2->getservicenew($id)->row_array();
$this->template->viewadmin2('admin2/transaks/updateservicenew',$data);
}
public function saveservicenew(){
$this->Madmin2->saveservicenew($this->input->post());
redirect(base_url('admin2/transaksidone'));
}

这是我的模型

public function getservicenew($id){     $this->db->select('kontak_sis.*,vendor.*,vendor2.*,service_sis.*,sumber_info.*,provinsi.*,kota.*,mst_service.*,service.*');
$this->db->from('service_sis');
$this->db->join('vendor', 'service_sis.id_vendor=vendor.id_vendor', 'left');
$this->db->join('vendor2', 'service_sis.id_vendor2=vendor2.id_vendor2', 'left');
$this->db->join('mst_service', 'service_sis.id_mst_service=mst_service.id_mst_service', 'left');
$this->db->join('kontak_sis', 'service_sis.id_kontak=kontak_sis.id_kontak', 'left');
$this->db->join('sumber_info', 'service_sis.id_sumber=sumber_info.id_sumber', 'left');
$this->db->join('provinsi', 'service_sis.id_provinsi=provinsi.id_provinsi', 'left');
$this->db->join('kota', 'service_sis.id_kota=kota.id_kota', 'left');
$this->db->join('service', 'service_sis.id_service=service.id_service', 'left');
$param = array('service_sis.id_service'=>$id);
return $this->db->get_where('',$param);
}
public function simpanservicenew(){
$data_pelanggan['cid']= $this->input->post('cid');
$data_pelanggan['nama_perusahaan']= $this->input->post('nama_perusahaan');
$data_pelanggan['destination']= $this->input->post('destination');
$data_pelanggan['alamat_perusahaan']= $this->input->post('alamat_perusahaan');
$data_pelanggan['pic_perusahaan']= $this->input->post('pic_perusahaan');
$data_pelanggan['telepon_pic']= $this->input->post('telepon_pic');
$data_pelanggan['fax']= $this->input->post('fax');
$data_pelanggan['hp']= $this->input->post('hp');
$data_pelanggan['email_pic']= $this->input->post('email_pic');

$this->db->where('id_kontak',$this->input->post('id_kontak'));
$this->db->update('kontak_sis',$data_pelanggan);

$data_vendor['pic_vendor']= $this->input->post('pic_vendor');
$data_vendor['media_vendor']= $this->input->post('media_vendor');
$data_vendor['kapasitas_vendor']= $this->input->post('kapasitas_vendor');
$data_vendor['telepon_vendor']= $this->input->post('telepon_vendor');
$data_vendor['email_vendor']= $this->input->post('email_vendor');

$this->db->where('id_vendor',$this->input->post('id_vendor'));
$this->db->update('vendor',$data_vendor);

$data_vendor2['pic_vendor2']= $this->input->post('pic_vendor2');
$data_vendor2['media_vendor2']= $this->input->post('media_vendor2');
$data_vendor2['kapasitas_vendor2']= $this->input->post('kapasitas_vendor2');
$data_vendor2['telepon_vendor2']= $this->input->post('telepon_vendor2');
$data_vendor2['email_vendor2']= $this->input->post('email_vendor2');

$this->db->where('id_vendor2',$this->input->post('id_vendor2'));
$this->db->update('vendor2',$data_vendor2);

这是我的视图


    <div class="row">
    <div class="box box-primary">
    <div class="box-header with-border">
    <div id='progress'><div id='progress-complete'></div></div>
    <h2 class="box-title"><b>Set Ewo</b></h2>
    </div>
    <div class="box-body">
    <form method="post" action="<?php echo base_url('admin2/simpanservicebaru/'.$this->uri->segment(3)); ?>" enctype="multipart/form-data" id="myForm">
    <input type="hidden" name="id_service" value="<?php echo $service['id_service']; ?>">
    <input type="hidden" name="id_kontak" value="<?php echo $service['id_kontak']; ?>">
    <input type="hidden" name="id_mst_service" value="<?php echo $service['id_mst_service']; ?>">
    <input type="hidden" name="id_vendor" value="<?php echo $service['id_vendor']; ?>">
    <input type="hidden" name="id_vendor2" value="<?php echo $service['id_vendor2']; ?>">

    <fieldset>
                        <div class="form-group">
                            <label for="">Registration Form ID</label>
                            <input type="text" name="cid" class="form-control" placeholder="" value="<?php echo $service['cid'] ?>" readonly>
                        </div>
    <div class="form-group">
                            <label for="">Customer</label>
                            <b><input type="text" name="nama_perusahaan" class="form-control" value="<?php echo $service['nama_perusahaan']; ?>"required></b>
                        </div>
                        <div class="form-group">
                            <label for="">Destination</label>
                            <b><input type="text" name="destination" class="form-control" placeholder="destination" 
                            value="<?php echo $service['destination']; ?>"required></b>
                        </div>
                        <div class="form-group">
                            <label for="">Address</label>
                            <b><input type="text" name="alamat_perusahaan" class="form-control" placeholder="alamat" 
                            value="<?php echo $service['alamat_perusahaan']; ?>"required></b>
                        </div>
    <div class="form-group">
                            <label for="">PIC(Engineering)</label>
                            <input type="text" name="pic_perusahaan" class="form-control" placeholder="pic"
                            value="<?php echo $service['pic_perusahaan']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Phone Number</label>
                            <input type="text" name="telepon_pic" class="form-control" placeholder="phone number" 
                            value="<?php echo $service['telepon_pic']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Fax</label>
                            <input type="text" name="fax" class="form-control" placeholder="fax" 
                            value="<?php echo $service['fax']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">HP</label>
                            <input type="text" name="hp" class="form-control" placeholder="hp"
                            value="<?php echo $service['hp']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Email</label>
                            <input type="text" name="email_pic" class="form-control" placeholder="email pic" 
                            value="<?php echo $service['email_pic']; ?>">
                        </div>
    </fieldset>
                        <fieldset>
                        <div class="form-group">
                            <label for="">Vendor1</label>
                            <select name="id_vendor" class="form-control">
                            <option value="0">Non Vendor</option>

                        <?php
                        foreach ($vendor as $c)
                        { ?>
                          <?php $sel = ($c->id_vendor==$service['id_vendor']) ? 'selected' :''; ?>
                                 <option value="<?php echo $c->id_vendor;?>" <?php echo $sel;?>><?php echo $c->nama_vendor;?></option>
                        <?php }   ?>

                        </select>
                        </div>
                        <div class="form-group">
                            <label for="">PIC</label>
                            <input type="text" name="pic_vendor" class="form-control" placeholder="pic vendor" 
                            value="<?php echo $service['pic_vendor']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Media</label>
                            <input type="text" name="media_vendor" class="form-control" placeholder="media" 
                            value="<?php echo $service['media_vendor']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Capacity</label>
                            <input type="text" name="kapasitas_vendor" class="form-control" placeholder="kapasitas vendor" 
                            value="<?php echo $service['kapasitas_vendor']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Phone</label>
                            <input type="text" name="telepon_vendor" class="form-control" placeholder="telepon vendor" 
                            value="<?php echo $service['telepon_vendor']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Email</label>
                            <input type="text" name="email_vendor" class="form-control" placeholder="email vendor" 
                            value="<?php echo $service['email_vendor']; ?>">
                        </div>
                        <hr></hr>

                        <div class="form-group">
                            <label for="">Vendor2</label>
                            <select name="id_vendor2" class="form-control">
                            <option value="0">Non Vendor</option>

                        <?php
                        foreach ($vendor2 as $c)
                        { ?>
                          <?php $sel = ($c->id_vendor2==$service['id_vendor2']) ? 'selected' :''; ?>
                                 <option value="<?php echo $c->id_vendor2;?>" <?php echo $sel;?>><?php echo $c->nama_vendor2;?></option>
                        <?php }   ?>

                        </select>
                        </div>
                        <div class="form-group">
                            <label for="">PIC</label>
                            <input type="text" name="pic_vendor2" class="form-control" placeholder="pic vendor" 
                            value="<?php echo $service['pic_vendor2']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Media</label>
                            <input type="text" name="media_vendor2" class="form-control" placeholder="media" 
                            value="<?php echo $service['media_vendor2']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Capacity</label>
                            <input type="text" name="kapasitas_vendor2" class="form-control" placeholder="kapasitas vendor" 
                            value="<?php echo $service['kapasitas_vendor2']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Phone</label>
                            <input type="text" name="telepon_vendor2" class="form-control" placeholder="telepon vendor"
                            value="<?php echo $service['telepon_vendor2']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="">Email</label>
                            <input type="text" name="email_vendor2" class="form-control" placeholder="email vendor" 
                            value="<?php echo $service['email_vendor2']; ?>">
                        </div>
                        </fieldset>
                        <fieldset>
                        <br>
                            <p>*Pastikan semua form sudah terisi dengan benar</p>
                        <button id="submit" class="btn btn-success">Update Service</button>
                        </fieldset>

                </div>
            </div>
        </div>
    </div>
    <script>
       $( function() {
          var $signupForm = $( '#myForm' );
          $signupForm.validate({errorElement: 'em'});
          $signupForm.formToWizard({
          submitButton: 'submit',
          nextBtnName:  'Selanjutnya',
          prevBtnName:  'Sebelumnya',
          nextBtnClass: 'btn btn-primary btn-flat next',
          prevBtnClass: 'btn btn-default btn-flat prev',
          buttonTag:    'button',
          validateBeforeNext: function(form, step) {
              var stepIsValid = true;
              var validator = form.validate();
              $(':input', step).each( function(index) {
                  var xy = validator.element(this);
                  stepIsValid = stepIsValid && (typeof xy == 'undefined' || xy);
              });
              return stepIsValid;
          },
          progress: function (i, count) {
              $('#progress-complete').width(''+(i/count*100)+'%');
          }

          });
       });
    </script>

表格:

This is my **Table contact**
#  Name   type
1   id_kontak int(11)
2   cid    varchar(10)
3   nama_perusahaan    varchar(150)
4   destination    varchar(30)
5   alamat_perusahaan   varchar(200)
6   pic_perusahaan   varchar(200)
7   telepon_pic   varchar(30)
8   fax  int(15)
9   hp  int(15)
10  email_pic  (15)

This my **table vendor**
#  Name   type
1   id_vendor int(11)
2   pic_vendor     varchar(200)
3   telepon_vendor    varchar(100)
4   media_vendor    varchar(200)
5   kapasitas_vendor  varchar(200)
6   email_vendor   varchar(200)

This my **table vendor2**
#  Name   type
1   id_vendor2 int(11)
2   pic_vendor2     varchar(200)
3   telepon_vendor2    varchar(100)
4   media_vendor2    varchar(200)
5   kapasitas_vendor2  varchar(200)
6   email_vendor2   varchar(200)

我想一次更新,以便3个数据表也更新,因为表ID也位于另一个表中。谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

我从您的代码中了解到,您想使用单个查询更新3个表。在这种情况下,您可以使用以下代码。

$this->db->set('a.cid', $this->input->post('cid'));
$this->db->set('a.nama_perusahaan',  $this->input->post('nama_perusahaan'));

$this->db->set('b.pic_vendor', $this->input->post('pic_vendor'));
$this->db->set('b.media_vendor', $this->input->post('media_vendor'));

$this->db->set('c.pic_vendor2', $this->input->post('pic_vendor2'));
$this->db->set('c.companyaddress', $this->input->post('pic_vendor2'));

$this->db->where('a.id', 1);
$this->db->where('a.id = b.id');
$this->db->where('a.id = c.id');
$this->db->update('table as a, table2 as b',table3 as c);

但是您应该以更专业的方式使用Codeigniter Transactions。如果任何查询失败,则另一个查询将回滚。

    $this->db->trans_start();
    $this->db->query('AN SQL QUERY...');
    $this->db->query('ANOTHER QUERY...');
    $this->db->query('AND YET ANOTHER QUERY...');
    $this->db->trans_complete();