Bagaimana cara mengatasi primary key tidak bisa di update.

kalau di input bisa tapi di update tidak bisa

ini databasesnya..

nim sebagai primary key

CREATE TABLE `tbl_mahasiswa` (
  `nim` int(20) NOT NULL,
  `nama_mahasiswa` varchar(100) NOT NULL,
  `angkatan` int(5) NOT NULL,
  `jurusan` varchar(30) NOT NULL,
  `kelas_program` varchar(10) NOT NULL,
  PRIMARY KEY (`nim`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

ini coding controllernya..

 public function update($id)
    {
        $row = $this->Tbl_mahasiswa_model->get_by_id($id);

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('tbl_mahasiswa/update_action'),
		'nim' => set_value('nim', $row->nim),
		'nama_mahasiswa' => set_value('nama_mahasiswa', $row->nama_mahasiswa),
		'angkatan' => set_value('angkatan', $row->angkatan),
		'jurusan' => set_value('jurusan', $row->jurusan),
		'kelas_program' => set_value('kelas_program', $row->kelas_program),
	    );
            $this->load->view('tbl_mahasiswa/tbl_mahasiswa_form', $data);
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('tbl_mahasiswa'));
        }
    }

    public function update_action()
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update($this->input->post('nim', TRUE));
        } else {
            $data = array(
                  'nim' => $this->input->post('nim',TRUE),
		'nama_mahasiswa' => $this->input->post('nama_mahasiswa',TRUE),
	          'angkatan' => $this->input->post('angkatan',TRUE),
		      'jurusan' => $this->input->post('jurusan',TRUE),
		'kelas_program' => $this->input->post('kelas_program',TRUE),
	    );

            $this->Tbl_mahasiswa_model->update($this->input->post('nim', TRUE), $data);
            $this->session->set_flashdata('message', 'Update Record Success/Update Berhasil');
            redirect(site_url('tbl_mahasiswa'));
        }
    }

ini modedelnya

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Tbl_mahasiswa_model extends CI_Model
{

    public $table = 'tbl_mahasiswa';
    public $id = 'nim';
    public $order = 'DESC';


    function __construct()
    {
        parent::__construct();
    }

    // get all
    function get_all()
    {
        $this->db->order_by($this->id, $this->order);
        return $this->db->get($this->table)->result();
    }

    // get data by id
    function get_by_id($id)
    {
        $this->db->where($this->id, $id);
        return $this->db->get($this->table)->row();
    }

    // get total rows
    function total_rows($q = NULL) {
    $this->db->or_like('nim', $q);
	$this->db->or_like('nama_mahasiswa', $q);
	$this->db->or_like('angkatan', $q);
	$this->db->or_like('jurusan', $q);
	$this->db->or_like('kelas_program', $q);
	$this->db->from($this->table);
        return $this->db->count_all_results();
    }

    // get data with limit and search
    function get_limit_data($limit, $start = 0, $q = NULL) {
        $this->db->order_by($this->id, $this->order);
    $this->db->or_like('nim', $q);
	$this->db->or_like('nama_mahasiswa', $q);
	$this->db->or_like('angkatan', $q);
	$this->db->or_like('jurusan', $q);
	$this->db->or_like('kelas_program', $q);
	$this->db->limit($limit, $start);
        return $this->db->get($this->table)->result();
    }

    // insert data
    function insert($data)
    {
        $this->db->insert($this->table, $data);
    }

    // update data
    function update($id, $data)
    {
        $this->db->where($this->id, $id);
        $this->db->update($this->table, $data);

    }

    // delete data
    function delete($id)
    {
        $this->db->where($this->id, $id);
        $this->db->delete($this->table);
    }

}

ini view..

<!doctype html>
<html>
    <head>
        <title>harviacode.com - codeigniter crud generator</title>
        <link rel="stylesheet" href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css') ?>"/>
        <style>
            body{
                padding: 15px;
            }
        </style>
    </head>
    <body>
        <h2 style="margin-top:0px">Data Mahasiswa <?php echo $button ?></h2>
        <form action="<?php echo $action; ?>" method="post">


        <div class="form-group">
            <label for="int">Nim <?php echo form_error('nim') ?></label>
            <input type="text" class="form-control" name="nim" id="nim" placeholder="Nim" value="<?php echo $nim; ?>" />
        </div>
	    <div class="form-group">
            <label for="varchar">Nama Mahasiswa <?php echo form_error('nama_mahasiswa') ?></label>
            <input type="text" class="form-control" name="nama_mahasiswa" id="nama_mahasiswa" placeholder="Nama Mahasiswa" value="<?php echo $nama_mahasiswa; ?>" />
        </div>
	    <div class="form-group">
            <label for="int">Angkatan <?php echo form_error('angkatan') ?></label>
            <input type="text" class="form-control" name="angkatan" id="angkatan" placeholder="Angkatan" value="<?php echo $angkatan; ?>" />
        </div>
	    <div class="form-group">
            <label for="varchar">Jurusan <?php echo form_error('jurusan') ?></label>
            <input type="text" class="form-control" name="jurusan" id="jurusan" placeholder="Jurusan" value="<?php echo $jurusan; ?>" />
        </div>
	    <div class="form-group">
            <label for="varchar">Kelas Program <?php echo form_error('kelas_program') ?></label>
            <input type="text" class="form-control" name="kelas_program" id="kelas_program" placeholder="Kelas Program" value="<?php echo $kelas_program; ?>" />
        </div>

	    <button type="submit" class="btn btn-primary"><?php echo $button ?></button>
	    <a href="<?php echo site_url('tbl_mahasiswa') ?>" class="btn btn-default">Cancel</a>
	</form>
    </body>
</html>

avatar evanbljar
@evanbljar

1 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

Tanggapan

kayak nya $id yang di model bentrok deh .. antara yg di public sama yg d kirim dari controller

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban