title: MySQLI INSERT query is executed twice
body: <p>I have the following code:</p>
  
  <pre><code>// open the database connection
  require_once('php/db/connect.php');
  // set a log file
  $logFile = 'log/php.log';
  // set the error and the doCommit to false
  $error = false;
  $doCommit = false;
  // build the query
  $query = "SELECT name,gender,email,country FROM tmp_backer WHERE backerid = ?";
  // prepare the query
  if ($stmt = $mysqli-&gt;prepare($query)) {
      // bind the parameters
      $id = $postArray['backerid'];
      $stmt-&gt;bind_param('s',  $id);
      // execute the query
      if ($stmt-&gt;execute()) {
          // the query is executed!
          $stmt-&gt;store_result();
          $tmpArray = array();
          $stmt-&gt;store_result();
          $stmt-&gt;bind_result( $name,$gender,$email,$country);
          $stmt-&gt;fetch();
          // clear the parameters
          $query = '';
          // copy everything to the table 'backer'
          // build the query
          $query = "INSERT INTO backer (backerid,name,gender,email,country) VALUES (?,?,?,?,?)";
          // prepare the query
          if ($stmt = $mysqli-&gt;prepare($query)) {
              // bind the parameters
              $stmt-&gt;bind_param(
                  'ssiss',        
                  $id,
                  $name,
                  $gender,
                  $email,
                  $country
              );
              if ($stmt-&gt;execute()) {
                  // the query is executed!
                  // set doCommit to true
                  $doCommit = true;
              } else {
                  // the query could not be executed
                  // log this into the log file and send the visitor back to the become a backer page
                  error_log(date('Y-m-d H:i:s').' | query could not be executed (INSERT query) '.$stmt-&gt;error.PHP_EOL,3,$logFile);
                  $error = true;
              };
          } else {
              // the query is invalid
              // log this into the log file and send the visitor back to the become a backer page
              error_log(date('Y-m-d H:i:s').' | query is invalid (INSERT query) '.$stmt-&gt;error.PHP_EOL,3,$logFile);
              $error = true;
          };
      } else {
          // the query could not be executed
          // log this into the log file and send the visitor back to the become a backer page
          error_log(date('Y-m-d H:i:s').' | query could not be executed (SELECT query) '.$stmt-&gt;error.PHP_EOL,3,$logFile);
          $error = true;
      };
  } else {
      // the query is invalid
      // log this into the log file and send the visitor back to the become a backer page
      error_log(date('Y-m-d H:i:s').' | query is invalid (SELECT query) '.$stmt-&gt;error.PHP_EOL,3,$logFile);
      $error = true;
  };
  // check if there where any errors whilst preparing/executing
  // the query
  if ($error == true) {
      // there were errors
      $mysqli-&gt;rollback();
      exit();
  } else {
      // there were no errors
      // doCommit was set to true
      if ($doCommit == true) {
          $mysqli-&gt;commit();
      };
  };
  // close the db connection
  require_once('php/db/close.php');
  </code></pre>
  
  <p>The <strong>second</strong> query is executed twice, because I get an error in the log file containing the following text:</p>
  
  <blockquote>
    <p>2015-09-14 21:14:12 | query could not be executed (INSERT query) Duplicate entry 'qzdkzkdjzdk' for key 'PRIMARY'</p>
  </blockquote>
  
  <p>The <code>backerID</code> is a random key generated, and is the primary key (field: <code>backerid</code>) in both tables. The query WORKS, so the data is copied from the table tmp_backer to backer, but the problem is that the page is not shown (because the second time the INSERT query is executed, it encounters an error so it goes to the part where it is like: <code>$mysqli-&gt;rollback(); exit();</code></p>
  
  <p>Can anyone help me with this?</p>