• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Update and Flash-data messages not working

#1
It's been a long time since I logged in here.

I do not want to bother that much so I'm not going to be writing a lot instead I'm just going to tell my problem and ask for help.

Here is my problem I'm doing a new CodeIgniter application and I'm creating the admin panel. So far I've been able to create new "page_categories" into my database and I can fetch them with not problem but when I try to update a single item for example I want to change the name of "page category 2" to whatever I want

page category 1
page category 2

the name which updates is the page category 1 , does anybody know how to fix this?

Here is my controller:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Pages_categories extends CI_Controller {

    public function 
index(){
        
$data['subjects'] = $this->Pages_categories_model->get_list();

        
// Load template
        
$this->template->load('admin''default''pages_categories/index'$data);
    }

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

        if(
$this->form_validation->run() == FALSE){
            
// Load template
            
$this->template->load('admin''default''pages_categories/add');
        } else {
            
// Create Post Array
            
$data = array(
                
'name'    => $this->input->post('name')
            );

            
// Insert Subject
            
$this->Pages_categories_model->add($data);

            
// Activity Array
            
$data = array(
                
'resource_id'   => $this->db->insert_id(),
                
'type'            => 'subject',
                
'action'        => 'added',
                
'user_id'        => 1,
                
'message'        => 'A new page category was added ('.$data["name"].')'
            
);

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

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

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

    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');
        }
    }

    public function 
delete($id){
        
$name $this->Pages_categories_model->get($id)->name;

        
// Delete Subject
        
$this->Pages_categories_model->delete($id);

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

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

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

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


Here is my model:
PHP Code:
<?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 here is my view:
PHP Code:
<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 do Front-End development most of the time 
Reply

#2
What is the problem you faced when updating name?
Reply

#3
It looks ok to me. Perhaps you need to set the profiler to output on the page and see what queries are actually being run. Might help you to track down the problem. You may need to disable the redirect to check what was submitted and what query was executed.

In your model you have:
PHP Code:
    public function update($id, $data){
        $this->db->where('id', $id);
        $this->db->update($this->table, $data);
    

I would suggest that you check to see if it works and return TRUE or FALSE, then in your controller, only set the activity record if TRUE is returned, and some error if not,

PHP Code:
    public function update($id, $data)
    {
        $this->db->where('id', $id);
        if ($this->db->update($this->table, $data)) return TRUE;
           else return FALSE;
    

Also, does the activity record the correct id? Is the correct id appearing in your url for the form submit. Might help track down a problem too.

Not much help I know, sorry.

Paul.
Reply

#4
(11-17-2017, 08:21 AM)neuron Wrote: What is the problem you faced when updating name?

Sorry fro the late answer.
Rather than updating the ID that I select, it only updates the first one.
I do Front-End development most of the time 
Reply

#5
(11-17-2017, 11:33 AM)PaulD Wrote: It looks ok to me. Perhaps you need to set the profiler to output on the page and see what queries are actually being run. Might help you to track down the problem. You may need to disable the redirect to check what was submitted and what query was executed.

In your model you have:
PHP Code:
    public function update($id, $data){
        $this->db->where('id', $id);
        $this->db->update($this->table, $data);
    

I would suggest that you check to see if it works and return TRUE or FALSE, then in your controller, only set the activity record if TRUE is returned, and some error if not,

PHP Code:
    public function update($id, $data)
    {
        $this->db->where('id', $id);
        if ($this->db->update($this->table, $data)) return TRUE;
           else return FALSE;
    

Also, does the activity record the correct id? Is the correct id appearing in your url for the form submit. Might help track down a problem too.

Not much help I know, sorry.

Paul.

Yes the activity table records everything from adding a record, updating or deleting, it even records when I change an old name into a new one.
I do Front-End development most of the time 
Reply

#6
Do not forget also that CodeIgniter caches the queries.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
(11-18-2017, 05:40 AM)InsiteFX Wrote: Do not forget also that CodeIgniter caches the queries.

No it doesn't. Why is it that every week I have to point out that you're posting something ridiculous?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.