title: Why would my PHP code sometimes create multiple rows in my MySQL database?
body: <p>My MySQL db has 180k rows...7k of which are duplicates. Data is created via PHP (5.6.30) scripts run by a javascript web app doing POSTs.</p>
  
  <p>That is, they have the same data and the same time stamp.</p>
  
  <p>Here is the code that runs to create a row:</p>
  
  <pre><code>$log-&gt;lwrite('Do NOT have extCommentID / author: ' . $authorID);
               $query = "INSERT INTO anno_feedback (anno_fb_author_id, anno_fb_recip_ext_id, anno_fb_recip_ext_name, anno_fb_recip_avatar_url, anno_fb_ext_id, anno_fb_ext_sys_id, anno_fb_lib_id, anno_fb_group_id, anno_fb_comment_id, anno_fb_app_source, anno_fb_platform, anno_fb_use_custom, anno_fb_comment_text, anno_fb_url_target, anno_fb_word_target) VALUES ($authorID, '". $recipientID ."', '" .$recipientName . "', '" .$recipientAvatarURL . "', '" . $extCommentID . "', $sysID, $libID, $groupID, $commentID, '" . $appSource . "', '" . $platform . "',$commentUseCustom,'" . $commentText . "','" . $urlTarget ."','" . $wordTarget ."')";
               // $newFbID = mysqli_insert_id($connection);           //  Get ID of newly created row...
          //  //  echo "\n\nRunning query to create Group - $query";
           // $log-&gt;lwrite('New commentID created: ' . $newFbID);
      }       //  END else for no existing feedback
  
      //  NOW, regardless, run query...
      if(mysqli_query($connection, $query)){
          $newFbID = mysqli_insert_id($connection);           //  Get ID of newly created row...
          $log-&gt;lwrite('New commentID created: ' . $newFbID);
          echo "\n\nCreated/updated feedback row...for row: " . $newFbID;
      }   else {
          echo "ERROR: Was not able to execute $query. " . mysqli_error($connection);
      }
  </code></pre>
  
  <p>And a log example:</p>
  
  <blockquote>
    <p>[16/Oct/2018:07:13:27] (saveFeedback) -------------Starting save feedback for user / commentID: 5222 / 23093
    [16/Oct/2018:07:13:27] (saveFeedback) Do NOT have extCommentID / author: 5222
    [16/Oct/2018:07:13:27] (saveFeedback) New commentID created: 177444
    [16/Oct/2018:07:13:27] (saveFeedback) -------------Starting save feedback for user / commentID: 5222 / 23093
    [16/Oct/2018:07:13:27] (saveFeedback) Do NOT have extCommentID / author: 5222
    [16/Oct/2018:07:13:27] (saveFeedback) New commentID created: 177445</p>
  </blockquote>
  
  <p>These rows (177444 and 177445) are duplicates.</p>
  
  <p>I don't believe I have issues in the web app that runs the PHP through a POST command (not duplicate listeners or form submissions). I think the PHP is running twice sometimes - maybe concurrency or server load issues?</p>
  
  <p>What strategies can a PHP/MySQL solution use to avoid this? I've read about INSERT IGNORE but I'd have to check against a text field AND a date field to know whether a duplicate was being created - which feels expensive.</p>
  
  <p>Any direction appreciated - there's probably some best practice that I'm ignorant of to assure single rows are created. Happy to provide more detail.</p>