title: How to Redirect user to some other page if Duplicate Entry Error Occurs?
body: <p>I am trying to redirect user to some other page when duplicate Entry Error Occurs. I am using 
  <code>Codeigniter</code></p>
  
  <p>. I have written the following code, but it displays the error message <code>Duplicate entry '1' for key 'semester_id'</code>.</p>
  
  <p>Kindly check the code and guide me.</p>
  
  <p>Thanks</p>
  
  <pre><code>public function insert_sports_sports($student_id, $squash, $football, $cricket, $hockey, $swimming, $semester_id)
  {
      $data= array (
  
              "squash"=&gt; $squash,
              "football"=&gt;$football,
              "cricket"=&gt;$cricket,
              "hockey"=&gt;$hockey,
              "swimming"=&gt;$swimming,
              "studentId"=&gt;$student_id,
              "semester_id" =&gt; $semester_id
  
      );  
  
      if ($this-&gt;db-&gt;insert("sports",$data))
      {
          return true;
          }
          else
          {
              return false;
              }
      }
  </code></pre>
  
  <p><strong>CONTROLLER</strong></p>
  
  <pre><code>---
  --
  --
      if($this-&gt;loginmodel-&gt;insert_sports_sports($student_id, $squash, $football, $cricket,
                                               $hockey, $swimming, $semester_id))
      {
          redirect('mycontroller/index');
          }
          else
          {
              $this-&gt;load-&gt;view('success');
              }
      }   
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>    if ($this-&gt;db-&gt;insert("sports",$data))
      {
          return true;
          }
          else
          {
              return false;
              }
      }
  
  
  catch (Exception $ex) {
  
          if($ex-&gt;message == "Duplicate entry '1' for key 'semester_id'")
          {
              redirect('success.php');    
  
              }       
  }
  }
  </code></pre>
  
  <p><strong><em>ERROR MESSAGE</em></strong></p>
  
  <pre><code>A Database Error Occurred
  
  Error Number: 1062
  
  Duplicate entry '1' for key 'semester_id'
  
  INSERT INTO `sports` (`squash`, `football`, `cricket`, `hockey`, `swimming`, `studentId`, `semester_id`) VALUES ('4', '4', '4', '4', '4', '42', '1')
  
  Filename: F:\xampp\htdocs\student_management2\system\database\DB_driver.php
  
  Line Number: 330
  </code></pre>