codeigniter中的错误更新数据

时间:2014-03-26 10:29:05

标签: php codeigniter sql-update

我在更新数据时遇到错误。我是Codeigniter的新手,无法弄清楚如何解决这个问题。错误显示是未定义的变量id_berita,tanggal,judul_berita,content。如何解决这个问题?

错误显示:

> Severity: Notice

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

Filename: berita/edit_berita.php

查看:

<?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>

型号:

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

控制器:

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

请帮我这样做。谢谢

3 个答案:

答案 0 :(得分:2)

您似乎将内容值分配给名为data的单独数组,而不是类中的数据数组。

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

修改
好像你试图显示一个没有从帖子设置的值,或者请求不是一个帖子请求。我建议使用codeigniter函数set_value(),因此输入控件看起来像这样:

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

SET_VALUE()

允许您设置输入表单或textarea的值。您必须通过函数的第一个参数提供字段名称。第二个(可选)参数允许您为表单设置默认值。例如:

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

以上表格将显示&#34; 0&#34;第一次装的时候。

(资料来源:http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

答案 1 :(得分:0)

我不是CI程序员,但似乎您将未定义的变量传递给where,我会尝试这样:

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

答案 2 :(得分:0)

在控制器中尝试此操作

$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
模型中的

执行此操作

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