Tri Murvianto
Tri Murvianto

Reputation: 149

Codeigniter Database Error Number 1048 Values show NULL even though they are NOT NULL

I have situation where codeigniter shows database Error Number 1048. It seems Values NULL but when I try to check it usign var_dump($_POST) Values are not NULL.

Controller : Jurusan.php

public function simpan()
{
    $this->form_validation->set_rules('code','Kode','required|integer');
    $this->form_validation->set_rules('jurusan','Jurusan','required');
    $this->form_validation->set_rules('singkatan','Singkatan','required');
    $this->form_validation->set_rules('ketua','Ketua','required');
    $this->form_validation->set_rules('nik','NIK','required|integer');
    $this->form_validation->set_rules('akreditasi','Akreditasi','required');

    if($this->form_validation->run() == FALSE)
    {
        $isi['content'] = 'jurusan/form_tambahjurusan';
        $isi['judul'] = 'Master';
        $isi['sub_judul'] = 'Tambah Jurusan';
        $this->load->view('tampilan_home',$isi);
    } else {
        $this->model_security->getSecurity();

        $key = $this->input->post('code');
        $data['kd_prodi'] = $this->input->post['code'];
        $data['prodi'] = $this->input->post['jurusan'];
        $data['singkat'] = $this->input->post['singkatan'];
        $data['ketua_prodi'] = $this->input->post['ketua'];
        $data['nik'] = $this->input->post['nik'];
        $data['akreditasi'] = $this->input->post['akreditasi'];

        $this->load->model('model_jurusan');
        $query = $this->model_jurusan->getdata($key);

        if($query->num_rows()>0)
        {
            $this->model_jurusan->getupdate($key,$data);
        } else {
            $this->model_jurusan->getinsert($data);
        }

        redirect('jurusan');
    }   
}

Model : model_jurusan.php

class Model_jurusan extends CI_model {
    public function getdata($key)
    {
        $this->db->where('kd_prodi',$key);
        $hasil = $this->db->get('prodi');
        return $hasil;
    }

    public function getupdate($key,$data)
    {
        $this->db->where('kd_prodi',$key);
        $this->db->update('prodi',$data);
    }

    public function getinsert($data)
    {
        $this->db->insert('prodi',$data);
    }
}

Here is the error shown :

enter image description here

Here is the database structure :

enter image description here

Upvotes: 3

Views: 12193

Answers (4)

Abraham _Lundy
Abraham _Lundy

Reputation: 11

i have tried your code...it works. I think there some mistakes in your <input> tags, You must use <input name=""> not <input id=""> or something else. Hope it can help you out

Upvotes: 1

JMS786
JMS786

Reputation: 1109

You have a wrong syntax in these lines:

$key = $this->input->post('code');
$data['kd_prodi'] = $this->input->post['code']; // <-- use ('code')
$data['prodi'] = $this->input->post['jurusan']; // <-- use ('jurusan')

Change this to

$this->input->post['array_key'];

this

$this->input->post('array_key');

Read : Input Class in Codeigniter

Upvotes: 3

cyberrspiritt
cyberrspiritt

Reputation: 946

Well the problem lies in your way of accepting input parameters.

$this->input->post

is a method which accepts the variable name, not an array. So all the input parameters need to be passed as a function parameter to post method. These lines need to be altered to.

$data['kd_prodi'] = $this->input->post('code');
$data['prodi'] = $this->input->post('jurusan');
$data['singkat'] = $this->input->post('singkatan');
$data['ketua_prodi'] = $this->input->post('ketua');
$data['nik'] = $this->input->post('nik');
$data['akreditasi'] = $this->input->post('akreditasi');

Hope this solves the problem.

EDIT:

You did a var_dump($_POST) which works as it is supposed to and it will read the values of the post parameters. So either you fetch the parameters from $_POST array, or you use the $this->input->post() method. But I would suggest using the $this->input->post() method as it provides additional sanitization such as xss attack handling etc, which could be turned on an off from the config.

Upvotes: 1

Anand Pandey
Anand Pandey

Reputation: 2025

You are try to get value from post is wrong. You should use at this way

    $_POST['array value'];

Upvotes: 0

Related Questions