Welcome Guest, Not a member yet? Register   Sign In
double Insert into database [solved - it was Firefox]
#1

[eluser]xpix[/eluser]
Hi,

Form some reason the data I submit is entered twice into the database. I tried using a model or just simple code but the behavior is the same.

The profiler shows only 1 query and I tried to use a global counter to see how many time the insert function is called. The call happens once

I do not remember having this behavior 2 days ago. What could have change. As far as I know the code is ok.


Trying to insert through Phpmyadmin does not generate a double insert.


Here is my controller
Code:
class AdminSection extends Controller {

    
    function AdminSection()
    {
        parent::Controller();    
        
        if($this->session->userdata('login') != '1') {
            redirect('adminlogin');
        }
        
        $this->output->enable_profiler(TRUE);
        
        $this->css = $this->config->item('jb_admin_css');
        
    }
    
    function index()
    {
        $data['css'] = base_url().$this->css;
        
        $this->load->view('adminheader',$data);
        $this->load->view('adminmenu');
        $this->load->view('adminsection',$data);
        $this->load->view('adminfooter');
    }
    
    // ===============================// !PROGRAMS // ===============================
    
    
    function programs($action = '', $id = ''){
    
        //load model
        $this->load->model('Program');
        
        $data['css'] = base_url().$this->css;
        
        //action
        $data['action'] = $action;
        
        $data['editedProgramName'] = "";
        
        $data['errors'] = "";
        $data['messages'] = "";
        
        //display content - program table
        $data['content'] = "";
        
        //errors for second section
        $data['errors2'] = "";
        $data['messages2'] = "";
        
        
        
        if(!$action){
        
            $data['content'] = $this->Program->list_all();
            
        
        }else if($action == 'edit'){
            
            $data['editedProgram'] = $this->Program->get($id);

            
            $this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');
            
            if ($this->form_validation->run() == FALSE){
            
            }else{
            
                $result = $this->Program->update($id, $this->input->post('name'));
                
                if(!$result){
                    $data['errors2'] = "<p>An error has occured!</p>";
                }else{
                    $data['messages2'] = "<p>Success!</p>";
                }
            }
            
            $data['content'] = $this->Program->list_all();
            
            
        }else if($action == 'add'){
            
            $this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');
            
            if ($this->form_validation->run() == FALSE){
            
            }else{
            
                $result = $this->Program->add($this->input->post('name'));
                
                if(!$result){
                    $data['errors'] = "<p>An error has occured!</p>";
                }else{
                    $data['messages'] = "<p>Success!</p>";
                }
            }
            
            $data['content'] = $this->Program->list_all();
            
            //clear data
            
            $action = ''; $id = '';
        
        }else if($action == 'delete'){
            
            $result = $this->Program->delete($id);
                
            if(!$result){
                $data['errors2'] = "<p>An error has occured!</p>";
            }else{
                $data['messages2'] = "<p>Success!</p>";
            }
            
            $data['content'] = $this->Program->list_all();
        }
        
        $this->load->view('adminheader',$data);
        $this->load->view('adminmenu');
        $this->load->view('programs',$data);
        $this->load->view('adminfooter');
        
    }

}


Here's the model
Code:
&lt;?php

class Program extends Model {

    var $programs_table = 'jb_programs';
    
    function Program()
    {
        // Call the Model constructor
        parent::Model();
        
    }
    
    function list_all(){
                
        $query = $this->db->get($this->programs_table);
        
        $this->table->set_heading(array('Name', '', ''));
        
        foreach ($query->result() as $row){
            
            $edit = anchor($this->uri->slash_segment(1). $this->uri->slash_segment(2) . 'edit/' . $row->id, 'Edit');
            $delete = anchor($this->uri->slash_segment(1). $this->uri->slash_segment(2). 'delete/' . $row->id, 'Delete');
            
            $this->table->add_row(array($row->name, $edit, $delete));
        }
        
        return $this->table->generate();
        
    }
    
    function add($name){
                
        $data = array('name' => $name);
        
        return $this->db->insert($this->programs_table, $data);
        
    }
    
    function get($id){
    
        $query = $this->db->get_where($this->programs_table, array('id' => $id));
        return $query->row();
        
    }
    
    function update($id,$name){
    
        $data = array('name' => $name);
        $this->db->where('id', $id);
        return $this->db->update($this->programs_table, $data);
    
    }
    
    function delete($id){
    
        $data = array('id' => $id);
        return $this->db->delete($this->programs_table, $data);
    
    }  
    
        
}

?&gt;



Thank you
#2

[eluser]TheFuzzy0ne[/eluser]
Have you extended any libraries at all? It's almost as if the controller method is being run twice.
#3

[eluser]xpix[/eluser]
What do you mean by that?

I extended the Controller and the Model.

I auto load the following:

$autoload['libraries'] = array('database','form_validation','session','table');
$autoload['helper'] = array('url','form','security','html');
$autoload['plugin'] = array();
$autoload['config'] = array();
$autoload['language'] = array();
$autoload['model'] = array();


I have no clue why the any of my libraries/model are called twice.


Here's more info


It seems that the second insert is done after I display the data. So the first time you insert and list everything is OK.
But if you look in the database there are 2 items with the sam information.

So the second insert happens after the call $data['content'] = $this->Program->list_all(); from the }else if($action == 'add'){ section

This is even weirder.
#4

[eluser]Dam1an[/eluser]
Enable logging (level 2+) and then see the order the classes are initialised in, might give a clue of what happens twice
#5

[eluser]xpix[/eluser]
Could you be more specific on enabling logging. Never used it before. Thx
#6

[eluser]Dam1an[/eluser]
In config.php, there's a variable 'log_threashold', set this to the appropriate value (I use 4 during development)
The logs will be default be written to the log folder in the CI core (make sure its writable)
You will also need to log a few messages using log_message in you're own libraries/models/controller so you can follow the flow between CI stuff and yours
#7

[eluser]xpix[/eluser]
Thx guys for everything.

After enabling the logging I could clearly see that the page was loaded twice.

After restating firefox everything was ok. Smile
#8

[eluser]TheFuzzy0ne[/eluser]
Bad, Firefox! In your basket!




Theme © iAndrew 2016 - Forum software by © MyBB