views:

49

answers:

4

I decided that I would like to track the searches that users are making on my site, so I created a MySQL table called 'Searches' with a few simple fields, and a model (class) to facilitate the insertion and management of the saved searches.

The model function that saves the search is:

public function create() {
    $q = $this->db->query("INSERT INTO 
                                `searches` (
                                    `section`,
                                    `keywords`,
                                    `results`,
                                    `location`,
                                    `date`
                                ) VALUES (
                                    %s, 
                                    %s,
                                    %s,
                                    %s,
                                    %s
                                )",
                                $this->section,
                                $this->keywords,
                                $this->results,
                                $this->location,
                                date('Y-m-d G:i:s'));
    $this->id = $this->db->last_insert($q);
}

...and in my controller, when the page is loaded I go:

    /* lets save the search if there is one */
    if(isset($_GET['q'])) {
        $search = $this->load->model('Searches_Single',null);
        $search->set('section','sale');
        $search->set('keywords',$_GET['q']);
        $search->set('results',$grand_total);
        $search->set('location',$this->location->id);
        $search->create();
        unset($search);
    }

What is baffling me is that 1-4 duplicate rows are often being inserted instead of the just 1 that I expect.

There are no loops, no redirecting...just a very simple pageload. Frankly, I believe this happened to me the last time I attempted this.

UPDATE

This does not happen in my local development environment...only in production (scary). Both share the exact same code...could this be a server thing?

UPDATE NUMBER 2 I am finding that this seems to be happening on page opening and closing. AKA - I check the time the page opens, and the script logs a search then...and then I check the time the page finishes loading, and the script seems to be logging a search there as well...

GAHHH

+2  A: 

Your code example doesn't really give much insight on what might be causing this problem.

Are your search results paginated? If so, are you sure you are inserting the row only when those conditions are met?

  • The user is at page 1.
  • The user loaded page 1 for the first time.

It is possible that you are either inserting on each page load OR that you are inserted always at page 1, which causes duplicates if the user visits page 2 and decides to come back to page 1.

Andrew Moore
Andrew...yeah im sorry - there is just so much code that I tried to pick the most pertinent. To answer your question...I've been testing on searches that generate only one page of results anyway...about 2-3 results. So while I do paginate...it should theoretically not make a different...right?Please let me know if I can give more info that would help...I just didn't wanna post my whole page
johnnietheblack
A: 

The very best way to diagnose this (IMHO) is to simply log your db query to a file in /tmp at the time the query happens. You should be able to do this in your db class. If you do fopen/fwrite to log, make sure you use append "a" mode instead of "w" , or duplicate page requests will overwrite the log, and you won't be able to tell that the query is running multiple times. I've experienced several times where a page gets loaded 2-3 times that I don't expect due to some redirect I forgot about. The db query log should help you see if you have a malformed insert statement, or if a duplicate insert statement is running.

Zak
Zak, good idea...here's what I learned. Indeed the entire set of queries on the page as a whole are being run twice (or more) per load.There isn't a single redirect...and the queries are not in a loop:(
johnnietheblack
A: 

After diagnosing if sections of code are getting run multiple times, and you've determined there are no redirects, you can use debug_backtrace to see what the call stack is for your function. A controller my not be re-directing, but it could be calling a section of code multiple times in different locations. Use debug backtrace in a section that is getting called multiple times, then as you step out of the call stack, add a line of logging to say "entering function" before the call, and "leaving function" after the call.

Zak
A: 

i determined the problem is that google robots that follow a person to the page to determine which google ad to display are triggering the second pageload and therefore creating a mirrored db insertion. sometimes multiple times on a single page load.

sigh. cmon google.

johnnietheblack