title: How to prevent duplicate record inserts with PDO
body: <p>I want to try to setup something where if user already submitted a dept key and current date it will stop the record from being inserted. It will give out an error saying duplicate record entered try again.</p>
  
  <p>Here is part of my insert page but it is not working </p>
  
  <pre><code>$stmt= $db-&gt;prepare("INSERT INTO tbl_lighting(Department, 
      areaCode, offtime, gytime, ftime, ini, 
      sat_ob_department, sat_ib_department,
      sat_ob_onTime,sat_ib_onTime,
      sat_ob_offTime, sat_ib_offTime,
      ob_signature,ib_signature, 
      deptkey, comments,ib_comments,ob_comments,Requestdate
  
   ) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)WHERE NOT EXISTS 
  (SELECT * FROM tbl_lighting where deptkey = $deptkey AND Requestdate= CAST(GETDATE() AS DATE)");
  
  $stmt-&gt;bindParam(1, $_POST["Department"]);
  $stmt-&gt;bindParam(2, $_POST["areaCode"]);
  $stmt-&gt;bindParam(3, $_POST["offtime"]);
  $stmt-&gt;bindParam(4, $_POST["gytime"]);
  $stmt-&gt;bindParam(5, $_POST["ftime"]);
  $stmt-&gt;bindParam(6, $_POST["ini"]);
  $stmt-&gt;bindParam(7, $_POST["sat_ob_department"]);
  $stmt-&gt;bindParam(8, $_POST["sat_ib_department"]);
  $stmt-&gt;bindParam(9, $_POST["sat_ob_onTime"]);
  $stmt-&gt;bindParam(10, $_POST["sat_ib_onTime"]);
  $stmt-&gt;bindParam(11, $_POST["sat_ob_offTime"]);
  $stmt-&gt;bindParam(12, $_POST["sat_ib_offTime"]);
  $stmt-&gt;bindParam(13, $_POST["ob_signature"]);
  $stmt-&gt;bindParam(14, $_POST["ib_signature"]);
  $stmt-&gt;bindParam(15, $_POST["deptkey"]);
  $stmt-&gt;bindParam(16, $_POST["comments"]);
  $stmt-&gt;bindParam(17, $_POST["ib_comments"]);
  $stmt-&gt;bindParam(18, $_POST["ob_comments"]);
  $stmt-&gt;bindParam(19, $_POST["Requestdate"]);
  
  
  $stmt-&gt;execute();
  </code></pre>
  
  <p>Here is my submit form </p>
  
  <pre><code>&lt;form action = "insert_process.php" onsubmit="return validateForm()" name="form" id="form" method ="post" class="style1"&gt;
  
  &lt;table align="center" id="tfhover" class="tftable" border="1"&gt;
  &lt;br&gt;&lt;br&gt;
  &lt;tr&gt;
  &lt;td colspan="7"&gt;&lt;h1 align="center"&gt;Lighting Schedule Form&lt;/h1&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;th&gt;Department&lt;/th&gt;&lt;th style="width: 75px"&gt;Area Code&lt;/th&gt;&lt;th style="width: 144px"&gt;Off Time&lt;/th&gt;&lt;th&gt;Grave Yard On Time&lt;/th&gt;&lt;th&gt;First Shift OnTime&lt;/th&gt;&lt;th width="125px"&gt;Comments Or Date By&lt;/th&gt;&lt;th&gt;Initials&lt;/th&gt;&lt;/tr&gt;
  
  &lt;tr&gt;
  
      &lt;!--First row accross on the table--&gt;
  &lt;td&gt;&lt;select name="Department" id="Department" &gt;
              &lt;option value =""&gt;Please select ...&lt;/option&gt;
              &lt;option value ="Upstairs Hang East"&gt;Upstairs Hang East&lt;/option&gt;
              &lt;option value ="Upstairs Hang West"&gt;Upstairs Hang West&lt;/option&gt;
              &lt;option value ="RDR"&gt;RDR&lt;/option&gt;
              &lt;/select&gt;&lt;/td&gt;
  &lt;td style="width: 75px"&gt;&lt;input id="areaCode" name="areaCode" onkeydown="return false;" type="text"/&gt;&lt;/td&gt;
  &lt;td style="width: 144px"&gt;&lt;input class="offtime" id="offtime" name="offtime"  type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input id="gytime" name="gytime" type="text"&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input id="ftime" name="ftime" type="text" &gt;&lt;/td&gt;
  &lt;td&gt;&lt;input id="comments" name="comments" type="text"&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input id="ini" name="ini" type="text" style="width: 68px" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  
  &lt;input id="deptkey" name="deptkey" onkeydown="return false;" type="hidden"/&gt;
  
      &lt;!--end--&gt;
  </code></pre>