title: How to handle MySQL's Foreign Key Error in Php?
body: <p>I was testing foreign keys in MySQL. The objective is to prevent the entry of an <code>id</code> into <code>table2</code>, that does not exist in <code>table1</code>. I was hoping that using <code>foreign keys</code> would simply result in a row not being inserted, but it seems to throw up this huge error. How do I handle this in PHP? Is there a way to make MySQL simply not insert the row and return no rows returned?</p>
  <blockquote>
  <p>Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>wp-db</code>.<code>borrowed</code>, CONSTRAINT <code>borrowed_ibfk_1</code> FOREIGN KEY (<code>employeeid</code>) REFERENCES <code>employee</code> (<code>id</code>) ON DELETE CASCADE ON UPDATE CASCADE)' in C:\web\apache\htdocs\dev\foreign.php:10</p>
  <p>Stack trace:</p>
  <p>#0 C:\web\apache\htdocs\dev\foreign.php(10): PDOStatement-&gt;execute()</p>
  <p>#1 {main} thrown in C:\web\apache\htdocs\dev\foreign.php on line 10</p>
  </blockquote>