php - 无法在 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
我不明白错误在哪里,请帮助我。提前致谢。
解决方案
推荐阅读
- maven - Maven - spring boot war 和 uber jar 项目的多模块项目
- c# - 输入无效
- python - MongoDB - 将带有数组的一条记录转换为新集合中的多条记录
- java - 以编程方式将 FlowDroid 与 Soot 框架一起使用?
- jmeter - 我们可以安排 JMeter 为特定数量的用户运行一个小时吗?
- python - numpy:使用不同的列类型快速创建recarray
- c - 非阻塞延迟与阻塞延迟
- processing - 处理:在下面的代码中,x= x + 1 做了什么?
- list - 目录和子目录中的文件列表
- java - 如果在可能的情况下有一个 int 很重要,那么输入一个数字的最佳做法是什么?