【发布时间】:2013-11-03 13:01:28
【问题描述】:
我有一个表名“Category”,其中包含(cat_id、name、description)。我可以毫无问题地插入、检索和删除。但是当我更新我的类别时,我的数据库中没有插入任何数据。我检查了我的表,结果什么也没有。
POST 模型“Category_Model 扩展 CI_Model”:
public function custom_query($data)
{
$q = $this->db->query($data);
return $q;
}
POST 控制器“Category extends CI_Controller”:
public function edit_category()
{
$data['title'] = "Edit Category Page";
$this->load->view('edit_category', $data);
}
public function update_category()
{
$id = $this->input->post('cat_id'); // I try $id = $this->uri->segment(3); but no result
$name = $this->input->post('name');
$desc = $this->input->post('description');
$this->post_model->custom_query("update category set cat_name='".$name."', description='".$desc."' where cat_id='".$id."'"); // when I delete 'where cat_id='".$id."'' clause, all my records were changing/updating
// I change to $this->db->where('cat_id', $id); $this->db->update('category'), but no result.
redirect ('category/view_categories');
}
这是我的编辑类别视图:
<form action="<?php echo base_url(); ?>category/update_category" method="POST">
<fieldset>
<legend>Edit Category</legend>
<label for="cat">Name :</label>
<input type="text" name="name"/>
<label for="desc">Descriptions :</label>
<textarea name="description" cols="40" rows="2"></textarea>
<input type="submit" value="Update">
</fieldset>
</form>
请任何人告诉我我的代码有什么问题?提前致谢
最好的问候。
*注意:我将“数据库”放在自动加载配置中。
【问题讨论】:
-
您为什么希望
$this->db->where('cat_id', $id); $this->db->update('category');工作? -
我关注 CI 活动记录查询。你能告诉我会怎么样吗?
-
您缺少
set部分。尝试:$this->db->update('category', array('cat_name'=>$name, etc...), array('cat_id'=>$id));或将第二部分更改为$this->db->update('category', $setarray);请参阅here。 -
我使用这个自定义查询
$this->post_model->custom_query("update category set cat_name='".$name."', description='".$desc."' where cat_id='".$id."'");。我认为目标类似于 CI 用户指南 -
是的,是的,这对于原始查询(存在所有安全问题)来说似乎很好......我只是指您注释掉的部分。
标签: php mysql codeigniter