my code does not update the database, even though there is no error at all. please help me.
Controllers code
public function update(){
$id = $this->request->getpost('nim');
$data =[
'nama' => $this->request->getpost('nama'),
'kelamin' => $this->request->getpost('kelamin'),
'tempatlahir' => $this->request->getpost('tempat'),
'tanggallahir' => $this->request->getpost('tanggal'),
'alamat' => $this->request->getpost('alamat'),
'telepon' => $this->request->getpost('telp')
];
$siswa = new Modelsiswa();
$update = $siswa -> updatedata($data, $id);
if($update){
return redirect()->to('/siswa');
}
}
Views code
<body>
<?=form_open('siswa/update') ?>
<table>
<tr>
<td>NIM: </td>
<td>
<input type="text" name="nim" maxlength="7" readonly value="<?= $nim?>">
</td>
</tr>
<tr>
<td>Nama Lengkap</td>
<td>
<input type="text" name='nama' size='50' value="<?= $nama?>">
</td>
</tr>
<tr>
<td>jenis kelamin: </td>
<td>
<input type="radio" name="kelamin" value="L" <?php if($kelamin=='L')echo "checked";?> >Laki-Laki
<input type="radio" name="kelamin" value="P" <?php if($kelamin=='P')echo "checked";?> >Perempuan
</td>
</tr>
<tr>
<td>Tempat/Tanggal Lahir</td>
<td>
<input type="text" name="tempat" size="30" value="<?=$tempat?>"> / <input type="date" name="tanggal" value="<?=$tanggal?>">
</td>
</tr>
<tr>
<td>ALamat :</td>
<td>
<textarea name="alamat" id="" cols="50" rows="5"><?=$alamat?></textarea>
</td>
</tr>
<tr>
<tr>Telp: </tr>
<td>
<input type="text" name="telp" pattern="{0-9}+" value="<?=$telp?>">
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="simpan">
</td>
</tr>
</table>
<?=form_close();?>
</body>
Models Code for update
function updatedata($data, $id){
return $this->db->table('siswa')->update($data, ['id_siswa', $id]);
}
I've checked it many times but I don't think there's anything wrong with this code, but still can't update the database. how can i solve this?
My Answer:
>input->post('id');
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone')
);
$this->db->where('id', $id);
$this->db->update('users', $data);
redirect('users');
}
_Model code_
public function update_user($id, $data){
$this->db->where('id', $id);
$this->db->update('users', $data);
}
_View code_
Make sure that the form method is set to "post" and the action points to the correct controller method. Also, check if the input fields are correctly named and have the correct values. If everything seems correct, try to debug by echoing the SQL query generated by the update function to see if there are any issues with the query.
Rate this post
3 of 5 based on 5288 votesComments