1

I couldn't insert data into the database and no error display. I try var_dump($this->mberita->get_berita()); but array(0){}.

model:

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)
{
    $this->db->insert('berita_ukm', $data);
}

controller

function index()
{
    $this->data['berita'] = $this->mberita->get_berita();
    $this->data['title'] ='UKM Taekwondo | berita';
    $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
    $this->data['contents'] = $this->load->view('admin/berita/view_berita', $this->data, true);
    $this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}

function tambah_berita()
{
    if ($this->input->post('submit')) {
        $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) 
        {
            $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();
        } 
    }
    
    $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
  • try var_dump($this->db->get('berita_ukm')) in your model function get_berita() and see what you get Commented Mar 26, 2014 at 1:35

2 Answers 2

2

Seems you may be missing the data you want to insert:

$this->mberita->insert_berita($data);
Sign up to request clarification or add additional context in comments.

1 Comment

Is your insert code being executed? Type print_r($data); before $this->db->insert('berita_ukm', $data); and see if the array is output.
0

Your data is in the array data.But you don't pass it to the model.

So rewrite the code in controller as

$this->mberita->insert_berita($data);

5 Comments

make sure that you are in the if condition or in else part of the form validation
it's not working. i already edit controller tambah_berita you can see it and can you tell me where is wrong code?
you check enetred into the if condition of form validation.That is just print any thing in if loop
i try print_r($data) but the result is NULL.
you just give it in controller's else part

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.