dtf76989 2017-11-18 06:57
浏览 88
已采纳

更新和flashdata消息无法在CodeIgniter上工作

I've been working on a new CodeIgniter application and so far I've been able to create "page_categories" into the table, now my problem is when I try to update a certain category.

Let's say that I have two records on page_categories table:

enter image description here

Ok, now to be more specific when I try to change the name of "Page Category Two" rather than changing that name, my form updates the first record "Page Category One". Any idea how to fix this?

This is what I have on my controller:

public function edit($id){
    $this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[3]');

    if($this->form_validation->run() == FALSE){
        // Get Current Subject
        $data['item'] = $this->Pages_categories_model->get($id);

        // Load template
        $this->template->load('admin', 'default', 'pages_categories/edit', $data);
    } else {
        $old_name = $this->Pages_categories_model->get($id)->name;
        $new_name = $this->input->post('name');

        // Create Post Array
        $data = array(
            'name'  => $this->input->post('name')
        );

        // Insert Subject
        $this->Pages_categories_model->update($id, $data);

        // Activity Array
        $data = array(
            'resource_id'   => $this->db->insert_id(),
            'type'          => 'subject',
            'action'        => 'updated',
            'user_id'       => 1,
            'message'       => 'A page category ('.$old_name.') was renamed to ('.$new_name.')'
        );

        // Insert Activity
        $this->Activity_model->add($data);

        // Set Message
        $this->session->set_flashdata('success', 'Page category has been updated');

        //Redirect
        redirect('admin/pages_categories');
    }
}

and this is what I have on my model:

<?php
class Pages_categories_model extends CI_MODEL{
    function __construct(){
        parent::__construct();
        $this->table = 'pages_categories';
    }

    public function get_list(){
        $query = $this->db->get($this->table);
        return $query->result();
    }

    public function get($id){
        $query = $this->db->get($this->table);
        $this->db->where('id', $id);
        return $query->row();
    }

    public function add($data){
        $this->db->insert($this->table, $data);
    }

    public function update($id, $data){
        $this->db->where('id', $id);
        $this->db->update($this->table, $data);
    }

    public function delete($id){
        $this->db->where('id', $id);
        $this->db->delete($this->table);
    }
}

and finally my view:

<h2 class="page-header">Edit Page Category</h2>

<?php echo form_open('admin/pages_categories/edit/'.$item->id); ?>
    <div class="form-group">
        <?php echo form_label('Page Category Name', 'name'); ?>
        <?php
            $data = array(
                'name' => 'name',
                'id'    => 'name',
                'maxlength' => '100',
                'class'     => 'form-control',
                'value'     => $item->name,
            );
        ?>
        <?php echo form_input($data); ?>
    </div>

    <?php echo form_submit('mysubmit', 'Update Page Category', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>

I would like to say that my flash data message is not working as I expected. (It's not that important though.)

I want the flash-data message to be displayed in a different view; here it is my view:

<h2 class="page-header">Page Categories</h2>
<?php $CI =& get_instance(); if($CI->session->flashdata('success'){ ?>
           <div class="alert alert-success">
              <strong>Success!! </strong><?php echo $this->session->flashdata('success'); ?>
           </div>
     <?php } ?>
<?php if($this->session->flashdata('error')) : ?>
    <?php echo '<div class="alert alert-danger">'.$this->session->flashdata('error').'</div>'; ?>
<?php endif; ?>




<?php if($subjects) : ?>
<table class="table table-striped">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Date Created</th>
        <th></th>
    </tr>
    <?php foreach($subjects as $subject) : ?>
    <?php $date = strtotime($subject->create_date); ?>
    <?php $formatted_date = date('F j, Y, g:i a', $date); ?>
        <tr>
            <td><?php echo $subject->id; ?></td>
            <td><?php echo $subject->name; ?></td>
            <td><?php echo $formatted_date; ?></th>
            <td>
                <?php echo anchor('admin/pages_categories/edit/'.$subject->id.'', 'Edit', 'class="btn btn-default"'); ?>
                <?php echo anchor('admin/pages_categories/delete/'.$subject->id.'', 'Delete', 'class="btn btn-danger"'); ?>
            </td>
        </tr>
    <?php endforeach; ?>
</table>
<?php else : ?>
    <div class="alert alert-danger">No Page Category Found</div>
<?php endif; ?>

</div>
  • 写回答

2条回答 默认 最新

  • dream02008 2017-11-18 07:53
    关注

    Add the where clause before the query in this code in your model

    public function get($id){
        $this->db->where('id', $id);  // Give where clause before query
        $query = $this->db->get($this->table);
    
        return $query->row();
    }
    

    EDIT: Adding answer for flashdata

    Well, for flashdata, You have to call $_SESSION directly. Source: codeigniter documentation here

    so instead of

    <?php $CI =& get_instance(); if($CI->session->flashdata('success'){ ?>
    

    write

    <?php if(isset($_SESSION['success']) { ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题