Codeigniter将数据插入数据库

时间:2014-03-26 07:46:49

标签: php mysql codeigniter sql-insert

无法将数据插入mysql数据库。我混淆了错误的代码。我是Codeigniter的新手,无法弄清楚如何解决这个问题。

在下面查看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>

和模型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);
    }

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

请帮我做什么。谢谢

1 个答案:

答案 0 :(得分:1)

请使用此表达式:

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

而不是

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

但在config/autoload.php中加载帮助'form'之前,您可以像这样在控制器中手动加载:

$this->load->helper('form');