0

couldn't inserting data to mysql database. i confused where the wrong code. I am a newbie in Codeigniter and couldn't really figure out how to solve this.

below 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);
    }

please help me what to do. thank you

user3459150
  • 35
  • 2
  • 14
  • you have any error or something else and print_r($data) printing data or not – Dexter Mar 26 '14 at 07:50
  • what error you get? take a close look on the form's action url: `action="index.php/admin/berita/tambah_berita"` – jogesh_pi Mar 26 '14 at 07:50
  • no error but when i print_r($data) the result is NULL – user3459150 Mar 26 '14 at 07:53
  • your form redirecting to you your controller tambah_berita – Dexter Mar 26 '14 at 07: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? – user3459150 Mar 26 '14 at 07:58
  • try this instead of
    – Dexter Mar 26 '14 at 08:05
  • where we put that code after form or before form? – user3459150 Mar 26 '14 at 08:11
  • i try and when i klik button save error Cannot add or update a child row: a foreign key constraint fails (`tugas_akhir`.`berita_ukm`, CONSTRAINT `berita_ukm_ibfk_1` FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`)) INSERT INTO `berita_ukm` (`id_berita`, `tanggal`, `judul_berita`, `content`) VALUES ('34', '3/25/2014', 'putri', 'jfgjfk') – user3459150 Mar 26 '14 at 08:19
  • In the controller, before the line $this->mberita->insert_berita($data);" insert print_r($data); die; Please tell me what does it displays. – Pascut Mar 26 '14 at 08:43
  • your question is similar with this one: http://stackoverflow.com/questions/5005388/cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails , please display your table structure – Pascut Mar 26 '14 at 08:45
  • possible duplicate of [i couldn't insert data to database in codeigniter](http://stackoverflow.com/questions/22649818/i-couldnt-insert-data-to-database-in-codeigniter) – Adarsh M Pallickal Mar 26 '14 at 08:46
  • displays is Array ( [id_berita] => 34 [tanggal] => 3/25/2014 [judul_berita] => putri [content] =>nfdn) – user3459150 Mar 26 '14 at 08:53
  • but in below that display is database error – user3459150 Mar 26 '14 at 08:53

1 Answers1

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');
MatSnow
  • 7,357
  • 3
  • 19
  • 31
John Ro
  • 11
  • 4