title: why is my php pdo statement inserting twice in the DB
body: <p>i have an android app which is attempting to insert information to a database. i had a php REST API working perfectly fine WITHOUT PDO. i decided to try switching over to PDO prepared statements and i begun to experience some issues. i use to use mysql_query for inserting information to the database before i switched over to PDO. i have a feeling this is going to a be a very small and simple fix. please dont rip me too hard.</p>
  
  <p><strong>what ive read</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/40774057/pdo-inserting-twice">PDO Inserting twice</a></p>
  
  <p><a href="https://stackoverflow.com/questions/20987336/php-pdo-statement-error">PHP PDO statement error</a></p>
  
  <p><a href="https://stackoverflow.com/questions/21182407/url-rewrite-with-htaccess-make-duplicate-mysql-entries">URL rewrite with .htaccess</a></p>
  
  <p><strong>what ive done to debug</strong></p>
  
  <p>1) i have only executed each statement once. </p>
  
  <p>2) i have no rewrite rules in my .htaccess files : </p>
  
  <pre><code># Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
  RewriteBase /
  </code></pre>
  
  <p>^^that is all thats in my htaccess file</p>
  
  <p>3)  out of the two ($stmt and $sportStmt) i removed the second ($sportStmt) and the initial insert statement only inserted once, as desired. </p>
  
  <p>4) i checked on my app side to see whether i was making a post request twice, but i wasnt. (also evident when i removed the second insert statement)</p>
  
  <p>5) in case you are wondering if it is the table relations (pmk, fmk, triggers).. the script works perfectly fine before i decided to switch over to PDO prepared statements.</p>
  
  <p>this is my code:</p>
  
  <pre><code>    &lt;?PHP
  include_once("connection2.php");
  
  //prepare statement
      if (isset($_POST['sports'])){
  
      try {
      $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      // set the PDO error mode to exception
      $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
      switch ($_POST['sports']){
          case hockey:
          $stmt = $conn-&gt;prepare("INSERT INTO hockey (name, age, league) 
                                  VALUES (:name, :age, :league)");
          break;
          case soccer:
          $stmt = $conn-&gt;prepare("INSERT INTO soccer (name, age, league) 
                                  VALUES (:name, :age, :league)");
          break;
          case basketball:
          $stmt = $conn-&gt;prepare("INSERT INTO basketball (name, age, league) 
                                  VALUES (:name, :age, :league)");
          break;
          }
  
      $stmt-&gt;bindParam(':name', $name);
      $stmt-&gt;bindParam(':age', $age);
      $stmt-&gt;bindParam(':league', $league);
  
  
      //insert Sport
      $playerSports = $_POST['sports'];
      $playerSports = trim($playerSports, '[]');
      $sports = explode(',', $playerSports);
  
      foreach ($sports as $value){
          $value = trim($value, ' ');
          switch($value){
              case hockey:
                  $value = "hockey";
                  break;
              case soccer;
                  $value = "soccer";
                  break;
              case basketball;
                  $value = "basketball";
                  break;
          }
  
          $sportStmt = $conn-&gt;prepare("INSERT INTO playerSports (name, sports) VALUES (:name, :sports)
                                      ON DUPLICATE KEY UPDATE name = VALUES(name), sports = VALUES(sports);");
          $sportStmt-&gt;bindParam(':sports', $value);
          $sportStmt-&gt;bindParam(':name', $name);
          $name = $_POST['name'];
          $sportStmt-&gt;execute();
      }
  
  
      echo "New records created successfully";
      }
  catch(PDOException $e)
      {
      echo "Error: " . $e-&gt;getMessage();
      }
  
      } else {
          echo "did not receive category";
      }
  
  
  
  $conn = null;
  ?&gt;
  </code></pre>