title: Why is my PHP query executing twice on page load?
body: <p>I am newish to PHP and I seem to be having an issue with an insert statement that executes twice when I open this page to view a document. The document shows with no error. In the database the 2nd insert is 1 second later. It happens in google chrome only and on this page only. IE has no issue, I dont have firefox to check.</p>
  
  <p><strong>view_document.php</strong></p>
  
  <pre><code>&lt;?php
  require_once($_SERVER['DOCUMENT_ROOT'] . '/../includes/core.php');
  require_once($_SERVER['DOCUMENT_ROOT'] . '/../includes/connect.php');
  
  $webusername = $_SESSION['webname'];
  
  if (isset($_GET['document']))
  {
      $ainumber = (int) $_GET['document'];
  
      if (!ctype_digit($_GET['document']) || !preg_match('~^[0-9]+$~', $_GET['document']) || !is_numeric($_GET['document']))
      {
          $_SESSION = array();
          session_destroy();
          header('Location: login.php');
      }
      else
      {
          $stmt = $connect-&gt;prepare("SELECT s_filename, s_reference FROM dmsmain WHERE s_ainumber = ?") or die(mysqli_error());
  
          $stmt-&gt;bind_param('s', $ainumber);
          $stmt-&gt;execute();
          $stmt-&gt;bind_result($filename, $reference);
          $stmt-&gt;fetch();
          $stmt-&gt;close();
  
          $file = $_SERVER['DOCUMENT_ROOT'] . '/../dms/files/' . $filename . '.pdf';
  
          if (file_exists($file))
          {
              header('Content-Type: application/pdf');
              header('Content-Disposition: inline; filename=' . basename($file));
              header('Content-Transfer-Encoding: binary');
              header('Content-Length: ' . filesize($file));
              header('Accept-Ranges: bytes');
              readfile($file);
  
              $stmt = $connect-&gt;prepare("INSERT INTO dmslog (s_reference, s_userid, s_lastactivity,    s_actiontype) VALUES (?, ?, ?, ?)") or die(mysqli_error());
  
              date_default_timezone_set('Africa/Johannesburg');
              $date = date('Y-m-d H:i:s');
              $actiontype = 'DL';
  
              $stmt-&gt;bind_param('ssss', $reference, $webusername, $date, $actiontype);
              $stmt-&gt;execute();
              $stmt-&gt;close();
          }
          else
          {
              $missing = "&lt;b&gt;File not found&lt;/b&gt;";
          }
              exit(0); // Correct Place?
      }
  }
  ?&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;link href="../CSS/dms.css" rel="stylesheet" type="text/css" /&gt;
  &lt;link href="../favicon.ico" rel="icon" type="image/x-icon" /&gt;
  &lt;title&gt;Denso Document Manager - View Document&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="container"&gt;
  &lt;div id="header"&gt;
  &lt;table width="100%" border="0" cellpadding="0" cellspacing="0"&gt;
  &lt;tr&gt;
  &lt;td width="28%"&gt;&lt;img src="../images/Logo.gif"/&gt;&lt;/td&gt;
  &lt;td width="43%"&gt;&lt;h2 style="color:#666" align="left"&gt;Denso SA Document Management System&lt;/h2&gt;&lt;/td&gt;
  &lt;td width="3%"&gt;&amp;nbsp;&lt;/td&gt;
  &lt;td width="25%" align="right"&gt;&lt;?php echo "Welcome $webusername &lt;input class=\"look\" type=\"button\" onclick=\"parent.location='logout.php'\" value=' Logout ' /&gt;" ?&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;br /&gt;
  &lt;table bgcolor="#6699CC" height="30px" width="100%" border="0" cellpadding="0" cellspacing="0"&gt;
  &lt;tr&gt;
  &lt;td width="20%" align="center"&gt;&lt;p style="color:#FFF"&gt;&lt;b&gt;Document Search&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;
  &lt;td width="20%" align="center"&gt;&lt;p style="color:#FFF"&gt;&lt;b&gt;Add Document&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  &lt;div id="content"&gt;
  &lt;?php echo $missing; ?&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  &lt;/div&gt;
  &lt;div id="footer"&gt;
  &lt;table width="100%" bgcolor="#6699CC"&gt;
  &lt;tr&gt;
  &lt;td height="25px"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;table width="100%" bgcolor="#000000"&gt;
  &lt;tr&gt;
  &lt;td height="25px"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My HTTP access records I assume</p>
  
  <pre><code>[15/Nov/2012:10:14:32 +0200] "POST /dms/search.php HTTP/1.1" 200    5783 "http://www.denso.co.za/dms/search.php" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11"
  [15/Nov/2012:10:14:33 +0200] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11"
  
  [15/Nov/2012:10:14:34 +0200] "GET /dms/view_document.php?document=8 HTTP/1.1" 200 2965 "http://www.denso.co.za/dms/search.php" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11"
  [15/Nov/2012:10:14:35 +0200] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11"
  </code></pre>
  
  <p>I have checked my <code>&lt;img src=''&gt;</code> links and I dont see a problem with them.</p>
  
  <p>The records indictate there is a favicon.ico request so I created a blank favicon and placed it in my public_html folder and linked it in the page like so <code>&lt;link href="../favicon.ico" rel="shortcut icon" type="image/x-icon" /&gt;</code></p>
  
  <p>Unfortunately that did not work as the statement still executes twice.
  I am unsure if it is a favicon issue as my upload page uses an insert query and it executes once.</p>
  
  <p>If someone could please tell me where I am going wrong or point me in the right direction I would be very grateful</p>