【问题标题】:Codeigniter Database Error Number 1048 Values show NULL even though they are NOT NULLCodeigniter 数据库错误号 1048 值显示为 NULL,即使它们不是 NULL
【发布时间】:2018-01-05 04:00:12
【问题描述】:

我遇到了 codeigniter 显示数据库错误号 1048 的情况。它的值似乎为 NULL,但当我尝试检查它时使用 var_dump($_POST) 值不为 NULL。

控制器: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_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);
    }
}

这是显示的错误:

这是数据库结构:

【问题讨论】:

    标签: php mysql database codeigniter


    【解决方案1】:

    您在这些行中有错误的语法:

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

    把这个改成

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

    这个

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

    阅读:Input Class in Codeigniter

    【讨论】:

    • 什么是 array_key ?我还是不明白。我认为我应该在$this-&gt;input-&gt;post[' '] 中设置** 输入名称表单 **
    • 在这种情况下我已经使用了 input_form_name。但输出仍然为 NULL
    • 问题不在于您使用的名称,而是您将“[ ]”与“( )”误用。在这一行中,您已经做了正确的事情$key = $this-&gt;input-&gt;post('code');,但下一行您使用$data['kd_prodi'] = $this-&gt;input-&gt;post['code'];。 "[ ]" 表示您正在访问 array 的数组键,而 $this-&gt;input-&gt;post 实际上是一个接受字符串作为参数的 method。所以,一个方法使用 "( )" $this-&gt;input-&gt;post('blabla')not "[ ]" $this-&gt;input-&gt;post['blabla'].
    • 谢谢。终于我意识到了。但是在我按照您的建议更改代码后,仍然无法将数据插入数据库。我仍然无法弄清楚我的代码有什么问题。
    • 最后,我弄清楚出了什么问题。谢谢你的帮助。 @JMS786
    【解决方案2】:

    问题在于您接受输入参数的方式。

    $this->input->post
    

    是一个接受变量名的方法,而不是一个数组。所以所有的输入参数都需要作为函数参数传递给 post 方法。这些行需要更改为。

    $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');
    

    希望这能解决问题。

    编辑:

    您执行了var_dump($_POST),它按预期工作,它将读取 post 参数的值。因此,要么从$_POST 数组中获取参数,要么使用$this-&gt;input-&gt;post() 方法。但我建议使用 $this-&gt;input-&gt;post() 方法,因为它提供了额外的清理功能,例如 xss 攻击处理等,可以从配置中打开或关闭。

    【讨论】:

    • 仍然无法找出错误 cz still can not insert data into database.
    • 最后,我弄清楚出了什么问题。感谢您的帮助。
    【解决方案3】:

    我已经尝试过您的代码...它有效。我认为您的&lt;input&gt; 标签中有一些错误,您必须使用&lt;input name=""&gt; 而不是&lt;input id=""&gt; 或其他东西。希望能帮到你

    【讨论】:

      【解决方案4】:

      您试图从帖子中获取价值是错误的。你应该这样使用

          $_POST['array value'];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-04
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多