0

I can't insert data into my MySQL database. I am confused about where the code is flawed.

View: tambah_berita.php

<form name="form" action="<?php echo base_url();?>index.php/admin/berita/tambah_berita" method="post">
    <div class="two fields">
        <div class="field">
            <label>ID_Berita</label>
            <div class="ui small left icon input">
                <input type="text" placeholder="ID" name="id_berita">
                <i class="text file outline icon"></i>
            </div>
        </div>
    </div>

    <div class="fours fields">
        <div class="field">
            <div class="ui vertical segment">
                <div class="date field">
                    <label>Tanggal</label>
                    <div class="ui small icon input left">
                        <input type="text" placeholder="xx/xx/xxxx" name="tanggal">
                        <i class="calendar icon"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="two fields">
        <div class="field">
             <label>Judul</label>
            <div class="ui small left icon input">
                <input type="text" placeholder="Nama Profil" name="judul_berita">
                <i class="text file outline icon"></i>
            </div>
        </div>
    </div>
    <div class="field">
        <label>Isi Berita</label>
        <textarea placeholder="Text" name="content"></textarea>
    </div>

    <input class="ui small blue submit button" name="submit" type="submit" value="Save">
    <input class="ui small basic button" type="reset" value="Reset">
</form>

and model mberita.php

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

and controller berita.php

function index()
{
    $this->data['berita'] = $this->mberita->get_berita();
    //var_dump($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()
{   
    $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() == false) {
        $this->data['contents'] = $this->load->view('admin/berita/tambah_berita', '', true);
    } else {
        $this->load->model('mberita');

        $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);
    }
        
    $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
    $this->load->view('template/wrapper/admin/wrapper_ukm', $this->data);
}
13
  • you have any error or something else and print_r($data) printing data or not Commented Mar 26, 2014 at 7:50
  • what error you get? take a close look on the form's action url: action="<?php echo base_url();?>index.php/admin/berita/tambah_berita" Commented Mar 26, 2014 at 7:50
  • no error but when i print_r($data) the result is NULL Commented Mar 26, 2014 at 7:53
  • your form redirecting to you your controller tambah_berita Commented Mar 26, 2014 at 7:55
  • i am sorry.i am newbie i don't understand how to redirecting to you your controller tambah_berita. can you tell me how? Commented Mar 26, 2014 at 7:58

1 Answer 1

1

Please use this expression:

echo form_open('admin/berita/tambah_berita');

instead of

<form name="form" action="<?php echo base_url();?>index.php/admin/berita/tambah_berita" method="post">

but before that load helper 'form' in config/autoload.php or you can manually load in your controller like this:

$this->load->helper('form');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.