Codeigniter Database Error Code 1048 Values โ€‹โ€‹Show NULL Even though They Are Not NULL

I have a situation where codeigniter shows database Error number 1048. Seems to be 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);
    }
}

      

Below is the error:

enter image description here

Here is the structure of the database:

enter image description here

+3


source to share


4 answers


You have incorrect syntax on 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 is

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

      


Read: Input Class in Codeigniter

+2


source


Ok, the problem lies in your way of accepting input parameters.

$this->input->post

      

is a method that takes a variable name, not an array. Thus, all input parameters must be passed as a function parameter to the post method. These lines should be changed 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've done var_dump($_POST)

, which works as expected and it will read the message parameter values. Therefore, either you choose parameters from an array $_POST

, or you use the method $this->input->post()

. But I would suggest using the method $this->input->post()

as it provides additional sanitation, such as handling xss attacks, etc., which can be disabled from configuration.

+1


source


I tried your code ... it works. I think there are some errors in the tags <input>

, you should use <input name="">

not <input id="">

or whatever. Hope this helps you.

+1


source


You are trying to get the value from the post, it is wrong. You must use along the way

    $_POST['array value'];

      

0


source







All Articles