codeigniter 4 cant update database

Viewed 221

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?

2 Answers

$this->request->getpost() correct use $this->request->getPost()

function updatedata($data, $id){
    return $this->db->table('siswa')->update($data, ['id_siswa', $id]);
}

The 'where' condition does not exist in the above model and you don't need to use return.

function updatedata($data, $id){
   $this->db->table('siswa')->update()->set($data)->where('id_siswa', $id]);
}

You need to update this code, array format is wrong in your model function

function updatedata($data, $id){
    return $this->db->table('siswa')->update($data, ['id_siswa'=> $id]);
}
Related