Double Log Entry in PHP / MySQL by Jason Ramsey on Tuesday, July 4th 2017

I've been trying to learn more about PHP and what I can do to help people with WordPress or other sites written in PHP. Another topic involved in this process is also learning more about MySQL. Today I discovered a strange issue with my MySql insert statement double posting when viewing my PHP page in Chrome.

Here is how to replicate the issue:

  1. Setup a PDO Connection ($conn)
  2. Prepare the Insert Statement (in my case it was a stored procedure)
     $stmt = $conn->prepare('CALL add_log(?)');
     $stmt->bindParam(1, 'request log test', PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 50);
  3. Run the PHP Page using Chrome (I also happened to be in iPhone emulator mode)
  4. Check Log Table and see Two records inserted
  5. Ponder life...

Googling the issue I discovered some interesting solutions other people use for database and 404 handlers, etc. In the end, I noticed someone saying their emulator was the issue. I loaded the site from my phone and sure enough only one request was logged.

FIX: Browser Requesting FavIcon

Found out that the browser actually requests the favicon as a seperate request. To fix the double inserts, I added the following to my inde:Fixed the issue by adding this to the top of my 404 handler:

 if($_SERVER['REQUEST_URI'] == '/favicon.ico') return false;

Hopefully this helps someone else!

Back to List Next Entry