Welcome Guest, Not a member yet? Register   Sign In
Codeigniter db->insert adding the records twice.
#1

[eluser]Unknown[/eluser]
Hello everyone,

Please help me, I am more confused than the chicken crossing the road.

I have some experience in PHP but I am new to Codeigniter. I went through the tutorials of CI before starting to use it in my project.

I am trying to insert records in the database (MySql) using the db->insert() query.

No matter what I do/change/edit/tweak the records are getting added twice.

I have broken it down to the simplest solution and I am completely stuck with no light at the end of the tunnel.

The Controller
class Page extends CI_Controller{
function __construct(){
parent::__construct();
}

private function init(){
$this->load->model('page_model');
}

public function index(){
echo 'index';
}


public function home(){
$this->init();
$this->page_model->add_user();
}

}


The Model
class Page_model extends CI_Model {

function __construct(){
parent::__construct();
$this->load->database();
}

public function add_user(){
$user = array(
'name' => 'warren'
);

$this->db->insert('users', $user );
}
}

I have tried all the possible solutions I can think of.
Fresh Download
Different Systems (Local and remote server)
Zend Server, Wamp Server, Lighttpd
Options like
$this->db->cache_delete_all();
$this->db->query('insert into ...');
$this->db->insert('users', array('name'=>'warren') );
die() right after the insert statement.
$this->db->save_queries = FALSE;


I have enabled the logger and I see it is outputting twice and I am not sure if this is right.

DEBUG - 2011-08-16 11:12:36 --> Config Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Hooks Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Utf8 Class Initialized
DEBUG - 2011-08-16 11:12:36 --> UTF-8 Support Enabled
DEBUG - 2011-08-16 11:12:36 --> URI Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Router Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Output Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Security Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Input Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Global POST and COOKIE data sanitized
DEBUG - 2011-08-16 11:12:36 --> Language Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Loader Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Controller Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Model Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Model Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Database Driver Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Final output sent to browser
DEBUG - 2011-08-16 11:12:36 --> Total execution time: 0.2564
DEBUG - 2011-08-16 11:12:36 --> Config Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Hooks Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Utf8 Class Initialized
DEBUG - 2011-08-16 11:12:36 --> UTF-8 Support Enabled
DEBUG - 2011-08-16 11:12:36 --> URI Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Router Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Output Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Security Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Input Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Global POST and COOKIE data sanitized
DEBUG - 2011-08-16 11:12:36 --> Language Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Loader Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Controller Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Model Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Model Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Database Driver Class Initialized
DEBUG - 2011-08-16 11:12:36 --> Final output sent to browser
DEBUG - 2011-08-16 11:12:36 --> Total execution time: 0.2533

I searched around and I couldn't find a solution to this silly problem.
I also didn't find many posts with this problem which indicates I am doing something wrong.

I have been stuck for 36 hours trying to find out what I am doing wrong.

If someone can please help me I will be very appreciative.

If this is a silly mistake, I apologize as I am new to CI

Thank you in advance.
#2

[eluser]bapobap[/eluser]
Maybe just out of interest, try this before the db->insert

log_message('ERROR', 'Page: '.$_SERVER['REQUEST_URI']);

just to make sure the controller is only being hit once, rather than twice. It's hard to tell but your code looks fine so I can only guess it's being called twice due to some problem elsewhere.
#3

[eluser]plain jane[/eluser]
Can you please try using

Code:
$this->db->last_query();

so that you get to know the actual query,

or may be problem is because its been called twice.

Thanks,
ci user
#4

[eluser]John_Betong_002[/eluser]
Hi warrenfrias13,

Welcome to the forum and please learn how to enclose all scripts in code tags. The code is easier to read and prevents some members from getting upset Smile

log file:
Code:
...
DEBUG - 2011-08-16 11:12:36—> Final output sent to browser
DEBUG - 2011-08-16 11:12:36—> Total execution time: 0.2564
...
DEBUG - 2011-08-16 11:12:36—> Final output sent to browser
DEBUG - 2011-08-16 11:12:36—> Total execution time: 0.2553
 
According to the error log file your application is being loaded twice.

I would start by removing all scripts until the application is only loaded once then gradually enable scripts until you pinpoint the problem.
 




Theme © iAndrew 2016 - Forum software by © MyBB