0

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);
}
1
  • FYI, your $data variable can be simply populated by passing a "key whitelist" to the post() method. $data = $this->input->post(['id_berita', 'tanggal', 'judul_berita', 'content']); Commented Sep 26 at 7:01

3 Answers 3

2

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

its same error Message: Undefined variable: id_berita,tanggal,judul_berita,content
Which file is that, and what files are those you posted code of?
So my guess is your validation fails and you don't pass that data to view again. Take what marabunta2048 said and put it after your else.
0

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

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.