无法在codeigniter中插入数据

时间:2014-03-25 14:12:54

标签: php codeigniter

我无法将数据插入数据库。我不知道问题出在哪里,但当var_dump($this->mberita->get_berita());结果为array(0){}时。我是Codeigniter的新手,无法弄清楚如何解决这个问题。

模型

function get_berita()
{
    $this->db->order_by('id_berita','asc');
    $data = $this->db->get('berita_ukm');
    return $data->result();
}

//untuk menambah berita
function insert_berita($data)
{
    $data = array(
            'id_berita' => $this->input->post('id_berita'),
            'tanggal' => $this->input->post('tanggal'),
            'judul_berita' => $this->input->post('judul_berita'),
            'content' => $this->input->post('content')          
        );

    $this->db->insert('berita_ukm', $data);
}

function validate_berita()
{
    $this->form_validation->set_rules('id_berita', 'Id Berita', 'required|numeric');
    $this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
    $this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
    $this->form_validation->set_rules('content', 'Content', 'required');

    if ($this->form_validation->run() == TRUE) {
        return TRUE;
    }
}

控制器

function tambah_berita()
{
    if ($this->mberita->validate_berita() == TRUE) {
        $this->mberita->insert_berita();
        redirect('admin/berita/tambah_berita');
    } 

    $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
    $this->data['contents'] = $this->load->view('admin/berita/tambah_berita', '', true);
    $this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}

请帮我做什么。谢谢。

1 个答案:

答案 0 :(得分:0)

<?php 
#in controller page# 
// notes : you should make validation in controller page 

    if ($this->input->post('send')){ //request submit 

    // make form validation 

        $this->form_validation->set_rules('id_berita', 'Id Berita', 'required|numeric');
        $this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
        $this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
        $this->form_validation->set_rules('content', 'Content', 'required');

    if ($this->form_validation->run() == TRUE) {
           // request data then put in array
           $data = array(
                'id_berita' => $this->input->post('id_berita'),
                'tanggal' => $this->input->post('tanggal'),
                'judul_berita' => $this->input->post('judul_berita'),
                'content' => $this->input->post('content')          
            );
      $this->mberita->insert_berita($data);
        }
    }

//模型页面

        function insert_berita($data)
    {
       $this->db->insert('berita_ukm', $data);
    }