首页 > 解决方案 > 无法在 CodeIgniter 中更新和删除?

问题描述

我是 CodeIgniter 的新手,刚刚在 youtube 上观看了教程并按照步骤操作。我无法编辑或删除我的数据库记录。我可以插入或获取记录,但更新和删除函数总是返回错误。这是我的代码

控制器:

function __construct()
{
    parent::__construct();
    $this->load->model('Danhmuc_model');
}

function xoadm()
{
   $id = $this->uri->segment(5);
   $this->Danhmuc_model->delete($id);
   $this->session->set_flashdata('mess',' Đã xóa thành công');
   redirect(admin_url('danhmuc/xemdm'));
}

function suadm()
{
    $data = array();
    $id = $this->uri->segment(5);
    $row = $this->Danhmuc_model->get_info($id);
    if($this->input->post())
    {
        $this->form_validation->set_rules('ten_dm',"Tên danh mục",'required');
        if($this->form_validation->run())
        {
            $tendm = $this->input->post('ten_dm');
            $input = array('ten_dm'=>$tendm);
            $this->Danhmuc_model->update($id,$input);
            $this->session->set_flashdata('mess',' Đã sửa thành công');
        }
    }
    $data['row'] = $row;    
    $data['temp'] = 'admin/danhmuc/suadm';
    $this->load->view('admin/index',$data);
}

模型:

function update($id, $data)
{
    if (!$id)
    {
        return FALSE;
    }

    $where = array();
    $where[$this->key] = $id;
    $this->update_rule($where, $data);

    return TRUE;
}
/**
 * Xoa row tu id
 * $id : gia tri cua khoa chinh
 */
function delete($id)
{
    if (!$id)
    {
        return FALSE;
    }
    //neu la so
    if(is_numeric($id))
    {
        $where = array($this->key => $id);
    }else
    {
        //xoa nhieu row
        //$id = 1,2,3...
        $where = $this->key . " IN (".$id.") ";
    }
    $this->del_rule($where);

    return TRUE;
}

视图:编辑视图:

<td>
    <input name="title" class="form-control" type="text" value="<?php echo $row['ten_dm'] ?>">
</td>

如果我使用这个value="<?php echo $row['ten_dm'] ?>",文本框不会出现类别的标题(我正在插入和编辑类别) https://imgur.com/lW7FeDA https://imgur.com/Z6TjnhL

但是如果我使用这个value="<?php echo $row->ten_dm ?>",这个消息会出错

遇到 PHP 错误 严重性:通知

消息:试图获取非对象的属性

文件名:danhmuc/suadm.php

行号:13

我不明白错误在哪里,请帮助我。提前致谢。

标签: phpmysqlcodeigniterupdates

解决方案


推荐阅读