У меня есть код как показано ниже, но когда я обновляю данные, данные не обновляются, но добавляют новые данные в базу данных
Как я могу обновлять / редактировать данные и не добавлять новые данные при сохранении?
Здесь я использую CodeIgniter версии 3.1.8, php версии 7.2.4
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');
}
}