CodeIgniter Forums
MySQL Inserting twice and not sure why - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: MySQL Inserting twice and not sure why (/showthread.php?tid=5191)

Pages: 1 2


MySQL Inserting twice and not sure why - El Forum - 01-10-2008

[eluser]bapobap[/eluser]
Hi everyone,

In my controller I load my library, called main and call the start_page function:
Code:
$this->load->library('main');
echo $this->main->start_page(); // doesn't return anything

In the main library I have the start_page function, which is:
Code:
function start_page()
{
$obj =&get;_instance();
$sql = 'INSERT INTO `site_requests` (`id`, `request`, `referer`, `ip`, `xip`, `type`, `user_agent`, `date`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);';
$obj->db->query($sql);
}

When I load the page, I have it print out the SQL query, which it does once but for some reason it inserts into the database twice.

Now I'm sure it's being a terrible programmer (I am trying!) but I don't know what it outputs it once but does the insert twice. Any ideas?


MySQL Inserting twice and not sure why - El Forum - 01-10-2008

[eluser]bapobap[/eluser]
Enabled the profiler and it only lists the insert query once as well.


MySQL Inserting twice and not sure why - El Forum - 01-10-2008

[eluser]JamesD[/eluser]
bapobap,
Is
Code:
$obj =&get;_instance();
a typo for the post or do you actually have that in your code? (the semi-colon)

Edited: Disregard... it looks like the post is adding the extra symbol.

-JamesD


MySQL Inserting twice and not sure why - El Forum - 01-10-2008

[eluser]JamesD[/eluser]
Code:
function start_page()
{
$obj =&get;_instance();
$sql = 'INSERT INTO `site_requests` (`id`, `request`, `referer`, `ip`, `xip`, `type`, `user_agent`, `date`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);';
$obj->db->query($sql);
}

Do you need the instance for the query?
Could you not use
Code:
$this->db->query($sql);
without the extra instance?

It looks like that's where it could be squeezing in.

-JamesD


MySQL Inserting twice and not sure why - El Forum - 01-10-2008

[eluser]bapobap[/eluser]
I'm executing the SQL query in the library, so the object isn't accessible from the library (yes, I really don't understand OOP quite yet!).

I did try it in the controller using $this but it still happened, displayed once, in the profiler once but executed twice.

I think I'm going to give up, I think hooks might help me. I basically just needed this insert to be run with every single page request.

Thanks by the way!


MySQL Inserting twice and not sure why - El Forum - 01-10-2008

[eluser]JamesD[/eluser]
[quote author="bapobap" date="1200010095"]I'm executing the SQL query in the library, so the object isn't accessible from the library (yes, I really don't understand OOP quite yet!).

I did try it in the controller using $this but it still happened, displayed once, in the profiler once but executed twice.

I think I'm going to give up, I think hooks might help me. I basically just needed this insert to be run with every single page request.

Thanks by the way![/quote]

Was there a particular reason why you set it up as a library instead of a model?
Don't think you'll have the problem if you were to set it up as a Model instead.

For example:

Code:
<?php

class Main_model extends Model {
    var $obj;

    function Main_model()
    {
        parent::Model();
        $this->obj =& get_instance();
    }

    function start_page()
    {
        $sql = 'INSERT INTO `site_requests` (`id`, `request`, `referer`, `ip`, `xip`, `type`, `user_agent`, `date`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);';
        return $this->db->query($sql);
    }

?>

And use something similar to
Code:
$this->load->model('main_model', '', TRUE);
to load it.

-JamesD


MySQL Inserting twice and not sure why - El Forum - 01-10-2008

[eluser]tonanbarbarian[/eluser]
if you had the code in the controller and it was executing twice as well then the issue may be with the controller
please post the controller code that is calling the library so we can see what is going on, be as complete as possible.

The issue may be the name of the methods (functions) in the controller
For example if you name a method one of the reserved words like load, view etc then the system can be a bit confused and it will do strange things


MySQL Inserting twice and not sure why - El Forum - 01-11-2008

[eluser]webmogul[/eluser]
I just started seeing this kind of behavior as well last night. I started with using a helper but thought that might be the problem and went with a Model and it still does a double or even triple insert on occasion. I dump out some log statements and it says it is only executing once but that db don't lie Smile


MySQL Inserting twice and not sure why - El Forum - 01-11-2008

[eluser]bapobap[/eluser]
I settled with a post controller hook and it works fine for me now. It was weird that it output the request once, the profiler claimed it had only done it once but the database had two entries.

Not sure what was happening!


MySQL Inserting twice and not sure why - El Forum - 01-11-2008

[eluser]tonanbarbarian[/eluser]
this may not be the issue but the only thing else I could think of when looking at your code is that you are creating an instance of the controller in the model object when you do not need to.
try removing it and seeing if the duplication goes away