我有如下代码,但是当我更新数据时,数据不会更新,但会在数据库中添加新数据
如何保存更新/编辑数据而不添加新数据?
这里我使用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');
}
}
答案 0 :(得分:0)
问题是您的nim
值为$key
,并将其发送到name
列。所以,它根据你的代码返回0.如果你将插入0返回数据而不是更新。以及更新部分中,您正在检查nim
字段中的name
值。
更新您的代码,如下所示
public function getdata($key){
$this->db->where('nim',$key);
$hasil = $this->db->get('tb_anggota');
return $hasil;
}
public function getupdate($key,$data){
$this->db->where('nim',$key);
$this->db->update('tb_anggota',$data);
}