user3463680
user3463680

Reputation: 3

error update data in codeigniter

I am getting error when I am updating data. I am a newbie in Codeigniter and couldn't really figure out how to solve this. error display is undefined variable id_berita, tanggal, judul_berita, content. how to solve this problem?

Error display :

> Severity: Notice

Message: Undefined variable: id_berita,tanggal,judul_berita,content

Filename: berita/edit_berita.php

View :

<?php echo form_open('admin/berita/edit_berita','name=form'); ?>
                    <?php echo validation_errors(); ?>
                    <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" value="<?php echo $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" value="<?php echo $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 Berita" name="judul_berita" value="<?php echo $judul_berita;?>">
                                <i class="text file outline icon"></i>
                            </div>
                        </div>
                    </div>


                    <div class="field">
                        <label>Isi Berita</label>
                        <textarea placeholder="Text" name="content" value="<?php echo $content;?>"></textarea>
                    </div>

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

Model :

function update_berita($id_berita,$data)
    {
        $this->db->where('id_berita', $id_berita);
        $this->db->update('berita_ukm', $data);
        return true;
    }

Controller :

function edit_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/edit_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->db->where('id_berita', $id_berita);
        $this->db->update($data);
        }

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

Please help me to do this. Thank you

Upvotes: 0

Views: 1185

Answers (3)

marabunta2048
marabunta2048

Reputation: 1111

You seem to be assigning the content values to a seperate array called data, not to the data array in your class.

$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')
     );

Should be:

$this->data['id_berita'] = $this->input->post('id_berita');
$this->data['tanggal'] = $this->input->post('tanggal');
$this->data['judul_berita'] = $this->input->post('judul_berita');
$this->data['content'] => $this->input->post('content');

edit
Seems like you are trying to display a value that didn't get set from post or the request wasn't a post request. Id recommend using the codeigniter function set_value(), so for your input controls that would look like this:

<input type="text" placeholder="Nama Berita" name="judul_berita" value="<?php echo set_value('id_berita');?>">

set_value()

Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:

<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />

The above form will show "0" when loaded for the first time.

(Source: http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html)

Upvotes: 2

Dexter
Dexter

Reputation: 1796

try this in controller

$id_berita = $this->input->post('id_berita');
$data = array(
                'tanggal' => $this->input->post('tanggal'),
                'judul_berita' => $this->input->post('judul_berita'),
                'content' => $this->input->post('content')
            );

$this->model_name->update_berita($id_berita,$data) // call the model update function

in model do this

function update_berita($id_berita,$data)
    {
        $this->db->where('id_berita', $id_berita);
        $this->db->update('berita_ukm', $data);
        return true;
    }

Upvotes: 0

skywalker
skywalker

Reputation: 826

I am not CI programmer, but seems like you are passing undefined variable to where, I would try like this:

$this->db->where('id_berita', $data['id_berita']);

Upvotes: 0

Related Questions