I am running an insert query that should insert one row. However, it runs the query twice, and I can't figure out why.

Prior to the code below, the page runs a select query to get data about an image to be shown on the page. If it gets a result, it creates the html to display the image. After that, it inserts a record into a different table to record that the image has been shown. See below.

$imgid = $intimgid
$sitecode = $strcode;
$viewdate = date('Y-m-d');
$viewtime = date('H:i:s');
$viewip = $_SERVER['REMOTE_ADDR'];
$viewagent = $_SERVER['HTTP_USER_AGENT'];

$statement = '';
$statement = "INSERT INTO table1 (field1, field2, field3, field4, field5, field6) VALUES ('".$imgid."', '".$viewdate."', '".$viewtime."', '".$sitecode."', '".$viewip."', '".$viewagent."')";

$mysqli = new mysqli($dbserver, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_error) {
	trigger_error('Database connection failed: '  . $mysqli->connect_error, E_USER_ERROR);
}
$res = $mysqli->query($statement);
mysqli_close($mysqli);

When the query above runs, it inserts two rows. It has to be running twice because I've seen where the seconds in the time field are different by one second. I just can't figure out what's causing it to run twice.

    I might try sticking something like this immediately before the call to the query() method:

    error_log("Calling query...".PHP_EOL.var_export(debug_backtrace(),1));
    

    Then watch your error log to see if it gets called twice, and if there is something about the backtraces that gives you a clue as to what it calling it.

      I originally thought it had to do with something in the insert query. However, by checking error logs (thanks for the suggestion), I found the cause of the problem, but I don't understand why it is causing it. Here's what is happening.

      I have a .html file that needs two images that are selected from a pool of images in a database. Therefore, I read the .html file into a string, get the images, then use preg_replace to replace certain strings on the page and output the page with the images on it.

      // Read in the html file -- php cannot be executed in the html file, so particular text strings will be replaced
      $rpl_string = file_get_contents($sourcefile);
      
      // Choose the images that need to be added to the page
      $ar_images = array();
      $ar_images[0] = 'img1';
      $ar_images[1] = 'img2';
      
      // Call the function that selects the images from the database
      // This function runs a select query to get the images
      // Then it logs that the images have been seen.  This is where I was previously getting two records inserted instead of one.
      $ar_img_results = images_from_db($ar_images);
      
      // Add the images to the html file
      $ar_pattern = array();
      $ar_replacement = array();
      
      $ar_pattern[0] = '/IMAGE01/';
      $ar_replacement[0] = $ar_img_results[0];
      
      $ar_pattern[1] = '/IMAGE02/';
      $ar_replacement[1] = $ar_img_results[1];
      
      // EVERYTHING WORKS CORRECTLY UP TO THIS POINT
      // After testing, everything is fine up to now. Only one record per image is logged, just like it is supposed to.
      
      // When I run this function, it causes each of the images to be logged a second time.
      echo preg_replace($ar_pattern, $ar_replacement, $rpl_string);
      

      Why would preg_replace cause the images to be logged twice and what can I do about it?

        Can you show us the relevant line(s) from the error log?

          The error logs that I have showed me errors in my coding. By cleaning up the coding, I ended up discovering that the preg_replace was making a second insert somehow. However, I don't have the logs from the database queries themselves.

            preg_replace() would not be touching the database at all (unless something was really, really broken on your web server 😉 ).

            Sounds to me like the image requests are at some point calling the same database code -- maybe via that call to images_from_db()?

              Write a Reply...