我有一个表名称“Category”,其中包含(cat_id、名称、描述)。我可以毫无问题地插入、检索和删除。但是当我更新类别时,数据库中没有插入任何数据。我检查了我的表,结果什么也没有。
POST 模型“Category_Model 扩展 CI_Model”:
public function custom_query($data)
{
$q = $this->db->query($data);
return $q;
}
POST Controller “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');
}
这是我的编辑类别 View :
<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>
请有人告诉我我的代码有什么问题吗?提前致谢
致以诚挚的问候。
*注意:我将“数据库”放入自动加载配置中。
最佳答案
首先,您确定表名写正确吗?
..."update kategori..."
如果可以,请在将查询发送到数据库之前尝试输出您的查询,如下所示:
$query = "update kategori set cat_name='".$name."', description='".$desc."' where cat_id='".$id."'";
error_log('My query: ' . print_r($query, true));
$this->post_model->custom_query($query);
然后,如果您在该查询中没有发现任何问题,请将其交给我们。
关于php - 我的更新查询在 Codeigniter 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19753214/