title: php mysql avoid multiple/duplicate insertions from a single insert query
body: <p>I am trying to insert one row into a sample table. But every time I run the webpage, multiple rows are getting inserted. When I execute the same query from phpmyadmin, I get only one row inserted, which is right. I've also tried IGNORE and setting a primary key.</p>
  
  <p><img src="https://i.stack.imgur.com/GMUZu.jpg" alt="DB Table Structure that am using"></p>
  
  <p>The following is a snippet of the insert code:</p>
  
  <pre><code>&lt;?php
  $dbhost = "localhost";
  $dbuser = "root";
  $dbpass = "admin";
  $dbname = "learnphp";
  //connection open
  $connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
  //testing for connection error
  if(mysqli_connect_errno())
  {
      die("Databse connection failed: ".mysqli_connect_error()."(".mysqli_connect_errno().")");
  }
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;title&gt;Insert into Database with php&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;?php
  $id = 1;
  $name = 'sam';
  $age  = 10;
  
  $query = "INSERT INTO first (id,name,age) VALUES ('{id}','{$name}', '{$age}')";
  $result = mysqli_query($connection,$query);
  if($result)
  {
      //Success
      //redirect_to("somepage.php");
      echo "Success".$result;
  }
  else
  {
      //Failure
      //$message = "inserttion failed";
      die("Database query failed.".mysqli_error($connection));
  }
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php
      //connection close
      mysqli_close($connection);
  ?&gt;
  </code></pre>