CodeIgniter Forums
Simple insert being inserted twice - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Simple insert being inserted twice (/showthread.php?tid=52744)



Simple insert being inserted twice - El Forum - 06-25-2012

[eluser]erikdrums[/eluser]
I might be overlooking something terribly obvious, but if I run the following code directly in a controller:
Code:
function advertising() {
    $project_id = $this->input->get('project_id');
    $this->db->insert('ads', array('headline' => 'headline', 'project_id' => $project_id));
  }
Two entries are made in the db??? What would cause this to be run twice?
Code:
echo $this->db->last_query();
only displays one insert also.


Simple insert being inserted twice - El Forum - 06-25-2012

[eluser]WanWizard[/eluser]
last_query() will always shows one query (namely the last one executed).

You could activate the profiler to see all queries executed.

If you see only one there as well, it could be that your page is requested twice (this can happen if you use the default rewrite rules and one of your assets, or a favicon, is missing). To see if this the case, check your CI and/or server logfiles.


Simple insert being inserted twice - El Forum - 06-25-2012

[eluser]erikdrums[/eluser]
Thank you for a very useful reply. All other inserts seemed to work so it twisted my head a bit.
It turnes out I had a html validation extension running for chrome that made the double request for only some functions.
I will be better suited to debug similar issues in the future because of your reply.

Thank you! Smile



Simple insert being inserted twice - El Forum - 08-05-2012

[eluser]phpfresher[/eluser]
May be your method adevertesing() is executed twice . Once before submit and after you click the submit button.
Before you submit why don't you add this type of code like
Code:
if($this->input->post('submit')){
       submit the data
}