【问题标题】:my update query didn't work on Codeigniter我的更新查询在 Codeigniter 上不起作用
【发布时间】: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-&gt;db-&gt;where('cat_id', $id); $this-&gt;db-&gt;update('category'); 工作?
  • 我关注 CI 活动记录查询。你能告诉我会怎么样吗?
  • 您缺少set 部分。尝试:$this-&gt;db-&gt;update('category', array('cat_name'=&gt;$name, etc...), array('cat_id'=&gt;$id)); 或将第二部分更改为$this-&gt;db-&gt;update('category', $setarray); 请参阅here
  • 我使用这个自定义查询$this-&gt;post_model-&gt;custom_query("update category set cat_name='".$name."', description='".$desc."' where cat_id='".$id."'");。我认为目标类似于 CI 用户指南
  • 是的,是的,这对于原始查询(存在所有安全问题)来说似乎很好......我只是指您注释掉的部分。

标签: php mysql codeigniter


【解决方案1】:

首先,你确定你写的表名正确吗?

..."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);

那么,如果您在该查询中没有发现任何问题,请将其提供给我们。

【讨论】:

  • 嗨 Bandydan,我在我的数据库“类别”中正确写入。我只是在这里打错字。我更新了上面的代码。
  • 我遵循你的伎俩,将这段代码放入 "error_log('My query: ' . print_r($query, true));" (不带双引号)结果是我什么也没看到
【解决方案2】:

看起来您的查询可能没有得到cat_id,因为我在传递视图中的任何地方都看不到它。尝试在 HTML 中包含 cat_id 的隐藏字段。这也可能比尝试通过 URI 段获取它更容易。

【讨论】:

  • 嗨,我在表单打开之前添加了像这样“”这样的隐藏元素,在字段集之前,它仍然不起作用
  • 当您使用类别数据加载表单时,您是否使用cat_id 填充value 属性?
  • 是的,我对 value="&lt;?php echo $cat-&gt;cat_id ;?&gt;"&gt; 的价值来自 foreach ($cats-&gt;result() as $cat)
  • 尝试从where cat_id='".$id."'"); 中删除引号,因为这是输出where cat_id='3'(字符串)而不是 where cat_id=3`(整数)。从可读性/可扩展性的角度来看,最好使用 Active Record 在模型内生成查询,并为您想要进行的每种类型的调用提供单独的函数。
【解决方案3】:

您可以了解 CI 模型,它将简化您的生活。 我相信,由于某种原因,重定向可能会在提交之前关闭您的连接......如果您使用模型对象,它就不会发生。

【讨论】:

  • 是的,我会的。但在这种情况下,我需要有人指导。如果您有任何建议或链接,将受到欢迎。 :-)
  • 请开始测试我的样本...我学会了扩展我发现的一些样本。
【解决方案4】:

模型的小样本...

在应用程序/模型上创建一个类,像这样

file "category_model.php"... 注意这个名称,因为 CI 对模型名称有很大的限制。必须使用相同的类名,但全部小写。

class Category_model extends CI_Model {

// your fields
var $id = null;
var $name = null;

// call parent constructor... it's essential
function __construct() {
    parent::__construct();
}

// create a set function, for fill all fields directly from get or post
function set($data) {
    $this->id = isset($data['id']) ? $data['id'] : null;
    $this->name = isset($data['name']) ? $data['name'] : null;
}

// execute update on database
function update($id) {
    $this->db->update('category', $this, array('id' => $this->id));
}
}

在控制器上,实例化并调用模型

$this->load->model('Category_Model', null, true);
$this->Category_Model->set($this->post());
$this->Category_Model->update();

在此之后,继续执行正常代码。

【讨论】:

    猜你喜欢
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 2015-10-24
    • 2017-05-10
    • 2015-01-19
    • 2012-08-18
    相关资源
    最近更新 更多