1

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

0

5 Answers 5

2

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.

1
  • 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 Mar 5, 2010 at 0:58
1

I was having a similar problem with extra rows being inserted. It worked ok when I running an INSERT query from the command line in Perl, but no PHP script I could write would insert just one row. I even went so far as to try a different physical server, and then a virtual machine, all trying the simplest possible code:

<?php
mysql_connect("localhost", "USER", "PASSWORD") or die(mysql_error());
mysql_select_db("ci3") or die(mysql_error());
mysql_query("INSERT INTO trialsets (text) VALUES('values')");
header( 'Location: http://5.87.12.89/~tsbertalan/done.html' ) ;

echo "done";

Eventually, I tried WGET-ing the URL for to my php script from my laptop, which only produced one new entry in PHPMyAdmin. I then had the genius idea of TRYING OTHER BROWSERS. Turned out, Chrome was uncouthly hitting the page several times for some reason. Putting a

header( 'Location: http://5.87.12.89/~tsbertalan/done.html' ) ;

line right after the mysql_query line solved the problem. I wouldn't even have noticed this if I had followed good practice and redirected to a landing page immediately after processing POST input, as I eventually intended to do.


0

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.

1
  • 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:( Mar 5, 2010 at 2:19
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.