Please help me to fix this codeigniter php code because it can not update the data : I have the code like below, but when I update the data, the data is not updated but add new data in the database

How can I make updates / edit data and not add new data when saved?

Here I use codeigniter version 3.1.8, php version 7.2.4

Please help me to solve this problem, thanks


model_anggota.php

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Model_anggota extends CI_model {
      public function getdata($key){
        $this->db->where('nama',$key);
        $hasil = $this->db->get('tb_anggota');
        return $hasil;
      }
    
      public function getupdate($key,$data){
        $this->db->where('nama',$key);
        $this->db->update('tb_anggota',$data);
      }
    
      public function getinsert($data){
    
        $this->db->insert('tb_anggota',$data);
      }
    
      public function getdelete($key){
    
        $this->db->where('nim',$key);
        $this->db->delete('tb_anggota');
      }
    }
    

data_anggota.php

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Data_anggota extends CI_Controller {
    
	    public function index()
	    {
	    	$this->model_security->getsecurity();
	    	$isi['content'] = 'data_anggota/tampil_dataanggota';
        $isi['data'] = $this->db->get('tb_anggota');
    		$this->load->view('tampilan_home',$isi);
    	}
    
      public function tambah(){
        $this->model_security->getsecurity();
    		$isi['content'] = 'data_anggota/form_tambahanggota';
    
    		$this->load->view('tampilan_home',$isi);
      }
    
      public function edit(){
        $this->model_security->getsecurity();
    		$isi['content'] = 'data_anggota/form_editanggota';
        $key = $this->uri->segment(3);
        $this->db->where('nim',$key);
        $query = $this->db->get('tb_anggota');
        if($query->num_rows()>0){
          foreach ($query->result() as $row) {
            $isi['nama'] = $row->nama;
            $isi['nim'] = $row->nim;
            $isi['jurusan'] = $row->jurusan;
            $isi['tempat_lahir'] = $row->tempat_lahir;
            $isi['tanggal_lahir'] = $row->tanggal_lahir;
            $isi['alamat'] = $row->alamat;
            $isi['agama'] = $row->agama;
            $isi['kelamin'] = $row->kelamin;
            $isi['angkatan'] = $row->angkatan;
            $isi['status'] = $row->status;
          }
        }
        $this->load->view('tampilan_home',$isi);
      }
    

      public function simpan(){
        $this->model_security->getsecurity();
    
        $key = $this->input->post('nim');
        $data['nama'] = $this->input->post('nama');
        $data['nim'] = $this->input->post('nim');
        $data['jurusan'] = $this->input->post('jurusan');
        $data['tempat_lahir'] = $this->input->post('tempat_lahir');
        $data['tanggal_lahir'] = $this->input->post('tanggal_lahir');
        $data['alamat'] = $this->input->post('alamat');
        $data['agama'] = $this->input->post('agama');
        $data['kelamin'] = $this->input->post('kelamin');
        $data['angkatan'] = $this->input->post('angkatan');
        $data['status'] = $this->input->post('status');
    
        $this->load->model('model_anggota');
        $query = $this->model_anggota->getdata($key);
        if($query->num_rows()>0){
          $this->model_anggota->getupdate($key,$data);
          $this->session->set_flashdata('info','Data sukses diupdate');
        }else{
          $this->model_anggota->getinsert($data);
          $this->session->set_flashdata('info','Data sukses disimpan');
        }
        redirect('data_anggota');
      }

      public function delete(){
        $this->model_security->getsecurity();
        $this->load->model('model_anggota');
    
        $key = $this->uri->segment(3);
        $this->db->where('nim',$key);
        $query = $this->db->get('tb_anggota');
        if($query->num_rows()>0){
          $this->model_anggota->getdelete($key);
          $this->session->set_flashdata('info','Data sukses dihapus');
        }
        redirect('data_anggota');
      }

    }

