title: Codeigniter - Model - Duplicate Entry Alert
body: <p>made a simple subscription form submit email and city only email id i set unique but if email id is already in database it shows error i need to add alert here if data already in database alert to user but dont show the complete details like this.</p>
  
  <blockquote>
    <p>A Database Error Occurred</p>
    
    <p>Error Number: 1062</p>
    
    <p>Duplicate entry 'abc@abc' for key 2</p>
    
    <p>INSERT INTO <code>users</code> (<code>email</code>, <code>city</code>) VALUES ('abdullah@gaya.com',
    'jeddah')</p>
    
    <p>Filename:
    /home/content/f/a/h/fahadghafoor/html/fahad/models/users_model.php</p>
    
    <p>Line Number: 12</p>
  </blockquote>
  
  <p>Model file user_model.php</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
      class Users_model extends CI_Model
      {
  
  
      function create_member()
      {
              $new_member_insert_data = array(
                  'email' =&gt; $this-&gt;input-&gt;post('email'),
                  'city' =&gt; $this-&gt;input-&gt;post('city'),                           
              );
              $insert = $this-&gt;db-&gt;insert('users', $new_member_insert_data);
              return $insert;
  
  
      }//create_member
  }
  </code></pre>
  
  <p>Controller file user.php</p>
  
  <pre><code>&lt;?php 
  
  if (!defined('BASEPATH'))
      exit('No direct script access allowed');
  
  class User extends CI_Controller {
  
      function __construct() {
          parent::__construct();
          $this-&gt;load-&gt;helper('form');
          $this-&gt;load-&gt;helper('url');
          $this-&gt;load-&gt;library('user_agent');
          $this-&gt;load-&gt;library('form_validation');
      }
  
      public function create_user() {
          // field name, error message, validation rules
          $this-&gt;form_validation-&gt;set_rules('email', 'Email', 'trim|required|valid_email');
          $this-&gt;form_validation-&gt;set_rules('city', 'City', 'trim|required');
          if ($this-&gt;form_validation-&gt;run() == FALSE) {
              if ($this-&gt;input-&gt;post("lang") == "en") {
                  if ($this-&gt;agent-&gt;is_mobile()) {
                      $this-&gt;load-&gt;view('m_english_signup');
                  } else {
                      $this-&gt;load-&gt;view('d_english_signup');
                  }
              } //if ($this-&gt;agent-&gt;is_mobile())
              else {
                  if ($this-&gt;agent-&gt;is_mobile()) {
                      $this-&gt;load-&gt;view('m_arabic_signup');
                  } else {
                      $this-&gt;load-&gt;view('d_arabic_signup');
                  }
              }
          } else {
              $this-&gt;load-&gt;model('Users_model');
              if ($query = $this-&gt;Users_model-&gt;create_member()) {
                  if ($this-&gt;input-&gt;post("lang") == "en") {
                      if ($this-&gt;agent-&gt;is_mobile()) {
                          $this-&gt;load-&gt;view('m_english_thanks');
                      } else {
                          $this-&gt;load-&gt;view('d_english_thanks');
                      }
                  } else {
                      if ($this-&gt;agent-&gt;is_mobile()) {
                          $this-&gt;load-&gt;view('m_arabic_thanks');
                      } else {
                          $this-&gt;load-&gt;view('d_arabic_thanks');
                      }
                  }
              }
          }
      }
  
  }
  </code></pre>