title: How to solve SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry?
body: <p>This is my MySQL error.</p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 2
  </code></pre>
  
  <p>I googled it and read something about it, bud I couldn't understand.</p>
  
  <p>How to solve it?</p>
  
  <p>This is the main piece of <code>addStudent.php</code>:</p>
  
  <pre><code>require_once('../db.php');
  $db = new DB();
  if (isset($_POST['st_fname']) &amp;&amp; isset($_POST['st_lname']) &amp;&amp; isset($_POST['st_class']) &amp;&amp; isset($_POST['st_grade']))
  {
      $db-&gt;addStudent($_POST["st_fname"], $_POST["st_lname"], $_POST["st_class"], $_POST["st_grade"], $_POST["checkOlamp"]);
  }
  </code></pre>
  
  <p>and this is a part of <code>db.php</code>:</p>
  
  <pre><code>public function addStudent($fname, $lname, $classnum, $grade, $olamp)
  {
      $query = "INSERT INTO t_student (s_fname, s_lname, s_class, s_grade, s_olamp) VALUES('$fname', '$lname', '$classnum', '$grade', '$olamp');";
      $this-&gt;execute($query);
  }
  </code></pre>
  
  <p>And the t_student has a filed as <code>primary</code> key which is auto increment.</p>
  
  <ul>
  <li>db.php is something that I always use it instead of mysql_connection function in php, but I don't know what it is exactly. I know something that called "PDO" is used there.</li>
  </ul>