使用Codeigniter将数据更新为2个表

时间:2018-07-31 15:24:23

标签: javascript php json codeigniter

我正在尝试同时更新2个表tbl_tugas和tbl_nilai。这两个表具有相同的列,名为file_tugas。 我被选中并显示tbl_tugas中的记录,之后我可以编辑我的数据,但是我只能在tbl_tugas上编辑file_tugas。请帮助我,我想从tbl_tugas中获取一两个字段并插入到tbl_nilai中,并将新文件插入到文件tugas中的tbl_nilai中。 我希望你们能帮助我,谢谢

我的控制器:

public function data() {
    $data                   = $this->tugas_model->cari_semua($this->session->userdata('id_kelas'),$this->session->userdata('id_admin_ti'));     
    $hasil                  = array();
    $result                 = array();
    $nomor                  = 0;
    foreach ($data as $data) {
        $nomor              = $nomor + 1;
        $hasil[]            = array(
                'no'            => $nomor,
                'matkul'        => $data->matkul,               
                'kelas'         => $data->kelas,
                'dosen'         => $data->id_dosen,
                'nama'          => $data->nama_tugas,
                'deskripsi'     => $data->deskripsi_tugas,
                'file'          => $data->file_tugas,
                'action'        => '<div class="btn-group">
                                    <button id="btn-ubah" type="button" class="btn btn-warning btn-xs" 
                                                    data-id="'          . $data->id_tugas           . '" 
                                                    data-matkul="'      . $data->id_matkul          . '" 
                                                    data-kelas="'       . $data->id_kelas           . '" 
                                                    data-dosen="'       . $data->id_dosen           .'"
                                                    data-nama="'        . $data->nama_tugas         . '" 
                                                    data-deskripsi="'   . $data->deskripsi_tugas    . '" 
                                                    data-file="'        . $data->file_tugas         . '" 
                                                    ><i id="icon" class="fa fa-edit"></i></button>

                                    </div>'
            );
    }
    $result                 = array (
            'aaData'            => $hasil
        );
    echo json_encode($result);
}

public function edit() {
    $file_tugas = base_url()."assets/hasiltugas/".$_FILES['berkas']['name'];
    if (isset($_POST['nama']))  {
        $edit               = $this->tugas_model->edit(
                                $_POST['id'],
                                $_POST['matkul'],
                                $_POST['kelas'],
                                $_POST['dosen'],
                                $_POST['nama'],
                                $_POST['deskripsi'],
                                $file_tugas,
                                $_FILES['berkas']['name']
                            );
        $msg                = false;
        if ($edit) {
            $this->aksi_upload() ;
            $msg            = true;
        }
        echo json_encode($msg);
    }
}

我的模型:

    public function edit_mhs( $id, $matkul, $kelas, $nama, $deskripsi, $file) {
    $Querynilai     = $this->db->query(
                      "UPDATE nilai, tugas
                      SET tugas.id_matkul='$matkul', tugas.id_kelas='$kelas', tugas.nama_tugas='$nama', tugas.deskripsi_tugas='$deskripsi', nilai.file_tugas='$file'
                      WHERE nilai.id_tugas=tugas.id_tugas AND nilai.id_nilai='$id';"
                    );
    if ($Querynilai) {
        return TRUE;
    } else {
        return FALSE;
    };
}

我的观看

<div class="container body">
    <div class="main_container">
        <?php include(__DIR__ . "/../sidebar.php"); ?>
        <?php include(__DIR__ . "/../top_nav.php"); ?>

        <!-- page content -->

        <div class="right_col" role="main">
            <div class="row">
                <div class="col-md-12 col-sm-12 col-xs-12">

                    <div class="x_panel">
                        <div class="x_title">
                            <h2>
                                <a href="<?=base_url()?>">
                                    <i class="fa fa-home"></i>
                                </a> \ 
                                <a href="<?=base_url()?>tugas">
                                    Data Tugas
                                </a>
                            </h2>
                            <ul class="nav navbar-right panel_toolbox">
                            <!--    <li>
                                    <button type="button" id="btn-tambah" class="btn btn-primary">Tambah</button>
                                </li>
                                <li>
                                    <a class="collapse-link">
                                        <i class="fa fa-chevron-up"></i>
                                    </a>
                                </li> -->
                            </ul>
                            <div class="clearfix"></div>
                        </div>                      
                        <div class="x_content">
                            <table id="tabel-data" class="table table-bordered table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th style="vertical-align: middle;">No</th>
                                        <th style="vertical-align: middle;">Mata Kuliah</th>
                                        <th style="vertical-align: middle;">Kelas</th>
                                        <th style="vertical-align: middle;">Nama Tugas</th>
                                        <th style="vertical-align: middle;">Deskripsi</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody id="tabel-body">
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <!-- Tampilan "model-tambah" -->
                    <div class="modal fade" id="modal-tambah">
                        <form id="form-tambah" data-parsley-validate method="POST" action=""  enctype="multipart/form-data>
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal">×</button>
                                        <h4 class="modal-title">Tambah Data</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class="form-horizontal">
                                            <div class="form-horizontal">
                                            <form>
                                                <div class="form-group">
                                                    <label for="exampleFormControlFile1">Kumpulkan tugas anda dengan klik browse!</label>

                                                    <?php echo form_open_multipart('upload/aksi_upload');?>
                                                    <input type="hidden" id="id" name="id">                         
                                                    <input type="hidden" id="matkul" name="matkul">
                                                    <input type="hidden" id="kelas" name="kelas">
                                                    <input type="hidden" id="nama" name="nama">
                                                    <input type="hidden" id="dosen" name="dosen">
                                                    <input type="hidden" id="deskripsi" name="deskripsi">
                                                    <input type="file" class="form-control-file" id="file" name="berkas">
                                                    <input type="hidden" id="namafile" name="namafile">
                                                </div>
                                            </form>
                                        </div>
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <div class="btn-group">
                                            <button data-dismiss="modal" class="btn btn-warning" type="button">Batal</button>
                                            <button id="simpan" class="btn btn-success" type="button">Simpan</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                    <!-- Tampilan "model-tambah" -->
                </div>
            </div>
        </div>
        <!-- /page content -->

        <!-- footer content -->
        <footer>
            <div class="pull-right">
            MobiLé
            </div>
            <div class="clearfix"></div>
        </footer>
        <!-- /footer content -->
    </div>
</div>
<?php include(__DIR__ . "/../load_js.php"); ?>

<script>
    $(document).ready(function(){
        var tabel = $('#tabel-data').dataTable({
            "bProcessing":  true,
            "bAutoWidth":   true,
            "bSort":        false,
            "sAjaxSource":  '<?php echo base_url(); ?>tugas/data',
            "aoColumns":    [
                                { "mData"   : "no"},
                                { "mData"   : "matkul"},
                                { "mData"   : "kelas"},
                                { "mData"   : "nama"},
                                { "mData"   : "deskripsi"},
                                { "mData"   : "action"}
                            ],
            "columnDefs":   [
                                { className: "text-center", "targets": [0,5] },
                                { width: 30, targets: 0},//no
                                { width: 160, targets: 1},
                                { width: 50, targets: 2},
                                { width: 150, targets: 3},
                                { width: 50, targets: 5}//act
                            ],
            "fixedColumns": true
        });

        $('#btn-tambah').click(function(){
            $('#form-tambah').attr('action','<?=base_url()?>tugas/tambah');
            $('.modal-title').html('Tambah data');
            $('#modal-tambah').modal('show');
            $('#nama').focus();
        });

        $('#simpan').click(function() {
            $('#form-tambah').ajaxForm({
                success:    function(response){
                    if(response=='true'){
                        tabel.api().ajax.reload();
                        swal($('.modal-title').html() + ' Sukses');
                        $('#form-tambah')[0].reset();
                        $('#modal-tambah').modal('hide');
                        $('#icon').toggleClass('highlight');
                    }else{
                        swal($('.modal-title').html() + ' Gagal');
                    }
                },
                error: function(){
                    swal('ERROR : function(response)');
                }
            }).submit();
        });



        $('#tabel-body').on('click', '#btn-ubah', function(){
            $('.modal-title').html('Edit Tugas');
            $('#form-tambah').attr('action','<?=base_url()?>tugas/edit');
            $('#id').val($(this).data('id'));
            $('#matkul').val($(this).data('matkul'));
            $('#kelas').val($(this).data('kelas'));
            $('#dosen').val($(this).data('dosen'));
            $('#nama').val($(this).data('nama'));
            $('#deskripsi').val($(this).data('deskripsi'));
            $('#namafile').val($(this).data('file'));
            $('#modal-tambah').modal('show');
            $('#matkul').focus();
            $('#matkul').select();
        });

1 个答案:

答案 0 :(得分:0)

您可以尝试

public function edit_mhs( $id, $matkul, $kelas, $nama, $deskripsi, $file) {
    $Querynilai     = $this->db->query("UPDATE nilai SET nilai.file_tugas='$file'
                      WHERE nilai.id_nilai='$id'"
                    );
    $QueryTugas = $this->db->query("UPDATE tugas
                  SET tugas.id_matkul='$matkul', tugas.id_kelas='$kelas', tugas.nama_tugas='$nama', tugas.deskripsi_tugas='$deskripsi' WHERE nilai.id_nilai='matching_id'");
    if ($Querynilai === TRUE && $QueryTugas === TRUE ) 
     {
        return TRUE;
     } 
   else 
   {
        return FALSE;
    }