我已经添加,查看,删除记录但我无法更新数据库中的记录。但是当我尝试更新名称时,alamat等在数据库中保持不变。我在下面列出了我的控制器,型号和视图:
模型代码
function update($id,$siswa)
{
$this->db->where($this->primary_key,$id);
$this->db->update($this->table_name,$siswa);
}
控制器代码
function update($id=1)
{
$this->_set_fields();
$siswa = $this->siswa_model->get_by_id($id)->row();
$this->form_validation->id = $id;
$this->form_validation->nama = $siswa->nama;
if (isset($_POST['jenis_kelamin'])) {
$_POST['jenis_kelamin'] = strtoupper($data['siswa']['jenis_kelamin']);
}
if (isset($_POST['tanggal_lahir'])) {
$data['siswa']['tanggal_lahir'] = date('d-m-Y',strtotime($data['siswa']['tanggal_lahir']));
}
$data['title'] = 'Update siswa';
$data['message'] = '';
$data['action'] = site_url('siswa/updateSiswa');
$data['link_back']= anchor('siswa/index/','Back to list of siswas',array('class'=>'back'));
$this->load->view('siswaEdit',$data);
}
function updateSiswa()
{
//set common properties
$data['title'] = 'Update siswa';
$data['action'] = site_url('siswa/updateSiswa');
$data['link_back']= anchor('siswa/index/','Back to list of siswas',array('class'=>'back'));
//$this->load->library('form_validation');
//set validation properties
$this->_set_fields();
$this->_set_rules();
//$data['action']=('siswa/update/'.$id);
//run validation
if ($this->form_validation->run() === FALSE) {
$data['message']='';
$data['siswa'] = $this->siswa_model->get_by_id($id)->row_array();
if (isset($_POST['jenis_kelamin'])) {
$_POST['jenis_kelamin'] = strtoupper($data['siswa']['jenis_kelamin']);
}
if (isset($_POST['tanggal_lahir'])) {
$data['siswa']['tanggal_lahir'] = date('d-m-Y',strtotime($data['siswa']['tanggal_lahir']));
}
//set common properties
$data['title']='Update siswa';
$data['message']='';
}
else
{
//save data
$id = $this->input->post('id');
$siswa=array( 'id'=>$id,
'nama'=>$this->input->post('nama'),
'alamat'=>$this->input->post('alamat'),
'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
'tanggal_lahir'=>date('Y-m-d', strtotime($this->input->post('tanggal_lahir'))));
$this->siswa_model->update($id,$siswa);
$data['siswa'] = $this->siswa_model->get_by_id($id)->row_array();
//set user message;
$data['message']='update siswa success';
}
$data['link_back']= anchor('siswa/index/','Lihat Daftar Siswa',array('class'=>'back'));
//load view
$this->load->view('siswaEdit',$data);
}
查看代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SIMPLE CRUD APPLICATION</title>
<link href="<?php echo base_url();?>style/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="content">
<h1><?php echo $title;?></h1>
<div class="data">
<table>
<tr>
<td width="30%">ID</td>
<td><?php
if (isset($siswa->id)) {
echo $siswa->id;}
;?>
</td>
</tr>
<tr>
<td valign="top">Name</td>
<<td><?php
if (isset($siswa->nama)) {
echo $siswa->nama;}
;?>
</td>
</tr>
<tr>
<td valign="top">Alamat</td>
<td><?php
if (isset($siswa->alamat)) {
echo $siswa->alamat;}
;?>
</td>
</tr>
<tr>
<td valign="top">Jenis Kelamin</td>
<td><?php if (isset($siswa->jenis_kelamin)){
echo ($siswa->jenis_kelamin) =='M'?'Laki-Laki':'Perempuan';}
;?>
</td>
</tr>
<tr>
<td valign="top">Tanggal Lahir (dd-mm-yyyy)</td>
<td><?php if (isset($siswa->jenis_kelamin)){
echo date('d-m-Y',strtotime($siswa->tanggal_lahir));}
;?>
</td>
</tr>
</table>
</div>
<br/>
<?php echo $link_back;?>
</div>
</body>
</html>
你能帮助我从我的程序中找到解决问题的方法吗?
答案 0 :(得分:0)
我认为您在上面显示的视图页面,它只列出了记录。您可以在表单中列出以提交数据。