php - Codeigniter 博客中的文章未更新

标签 php mysql codeigniter

我是 codeigniter 的新手。我用 codeigniter 为博客构建了一个 cms。但是当我想要编辑一篇文章并想要保存它时,它会创建新的一篇文章而不是更新我编辑的旧文章。

请帮助我。

我的文章编辑crontroller:

public function edit ($id = NULL)
    {
        // Fetch a article or set a new one
        if ($id) {
            $this->data['article'] = $this->article_m->get($id);
            count($this->data['article']) || $this->data['errors'][] = 'article could not be found';
        }
        else {
            $this->data['article'] = $this->article_m->get_new();
        }


        // categories for dropdown
        $this->data['all_categories'] = $this->article_m->join();



        // Set up the form
        $rules = $this->article_m->rules;
        $this->form_validation->set_rules($rules);

        // Process the form
        if ($this->form_validation->run() == TRUE) {
            $data = $this->article_m->array_from_post(array(
                'title', 
                'extra_title', 
                'slug',
                'image',
                'category_id', 
                'body', 
                'pubdate'
            ));
            $this->article_m->save($data, $id);
            redirect('admin/article');
        }

        // Load the view
        $this->data['subview'] = 'admin/article/edit';
        $this->load->view('admin/_layout_main', $this->data);
    }

我的模型:

public function array_from_post($fields){
    $data = array();
    foreach ($fields as $field) {
        $data[$field] = $this->input->post($field);
    }
    return $data;
}

public function get($id = NULL, $single = FALSE){

    if ($id != NULL) {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key, $id);
        $method = 'row';
    }
    elseif($single == TRUE) {
        $method = 'row';
    }
    else {
        $method = 'result';
    }

    if (!count($this->db->ar_orderby)) {
        $this->db->order_by($this->_order_by);
    }
    return $this->db->get($this->_table_name)->$method();
}

public function get_by($where, $single = FALSE){
    $this->db->where($where);
    return $this->get(NULL, $single);
}

public function save($data, $id = NULL){

    // Set timestamps
    if ($this->_timestamps == TRUE) {
        $now = date('Y-m-d H:i:s');
        $id || $data['created'] = $now;
        $data['modified'] = $now;
    }

    // Insert
    if ($id === NULL) {
        !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;

        $this->db->set($data);
        $this->db->insert($this->_table_name);
        $id = $this->db->insert_id();
    }
    // Update
    else {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->set($data);
        $this->db->where($this->_primary_key, $id);
        $this->db->update($this->_table_name);
    }

    return $id;
}
public function get_new ()
{
    $article = new stdClass();
    $article->title = '';
    $article->extra_title = '';
    $article->slug = '';
    $article->image = '';
    $article->category_id = '';
    $article->body = '';
    $article->pubdate = date('Y-m-d');
    return $article;
}

public function join()
{
    $this->db->select('name,categories.id as category_id');
    $this->db->from($this->_table_name);
    $this->db->join('categories', 'categories.id = category_id','right');
    $Q = $this->db->get();
 if ($Q->num_rows() > 0){
   foreach ($Q->result_array() as $row){
     $data[$row['category_id']] = $row['name'];
   }
   }
   $Q->free_result();  
   return $data;
}

我的观点:

<?php echo validation_errors(); ?>
<?php echo form_open_multipart('admin/article/edit'); ?>
<table class="table">
    <tr>
        <td>Publication date</td>
        <td><?php echo form_input('pubdate', set_value('pubdate', $article->pubdate), 'class="datepicker"'); ?></td>
    </tr>
    <tr>
        <td>Category</td>
        <td><?php

        $js = 'id="category_id" onChange="some function();"';


         echo form_dropdown('category_id', $all_categories, set_value('category_id', $article->category_id), $js); ?></td>

    </tr>
    <tr>
        <td>Title</td>
        <td><?php echo form_input('title', set_value('title', $article->title)); ?></td>
    </tr>

    <tr>
        <td>Extra Title (Optional)</td>
        <td><?php echo form_input('extra_title', set_value('extra_title', $article->extra_title)); ?></td>
    </tr>

    <tr>
        <td>Slug</td>
        <td><?php echo form_input('slug', set_value('slug', $article->slug)); ?></td>
    </tr>

    <tr>
        <td>Upload Image</td>
        <td>
            <div class="input-append">
            <?php echo form_input('image', set_value('image', $article->image),'id="fieldID"'); ?>
                <a href="http://localhost/cms/filemanager/dialog.php?type=1&field_id=fieldID" class="btn iframe-btn" type="button">Select</a>
            </div>
        </td>
    </tr>

    <tr>
        <td>Body</td>
        <td><?php echo form_textarea('body', set_value('body', $article->body), 'class="tinymce"'); ?></td>
    </tr>
    <tr>
        <td></td>
        <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
    </tr>
</table>
<?php echo form_close();?>

请帮助我。

最佳答案

您必须将 id 传递给 Controller ​​。在您看来,您正在告诉要提交的表格 文章/编辑,但不是 id。

<?php echo validation_errors(); ?>
<?php echo form_open_multipart('admin/article/edit/'.$article->id); ?>
...

如果未设置 $article,您可能会收到一些警告,因此您可能需要在添加新项目时进行一些检查。

关于php - Codeigniter 博客中的文章未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21444867/

相关文章:

php - 从多维数组导入数据

mysql - SQL检查约束使值仅允许部分数字而其他部分仅允许字母

mysql - 如何在将记录异步放入运动流时确保顺序?

php - CodeIgniter 无法在本地主机上工作

php - 在 Modular Codeigniter 中,一个模块的 Controller 如何/应该如何调用另一个模块的函数?

php - 使用 OOP 在 wordpress 中添加 Action ?

php - 在我的网站上使用 Google Plus 登录

php - 获取cake 3 php中2个日期之间的数据结果

mysql - undefined symbol : mysql_options problem

php - Codeigniter 查询生成器,即使表中有数据,我也无法提取行并返回 null