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:
- Setup a PDO Connection ($conn)
- 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);
- Run the PHP Page using Chrome (I also happened to be in iPhone emulator mode)
- Check Log Table and see Two records inserted
- 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!