Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter - Model - Duplicate Entry Alert

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.

A Database Error Occurred

Error Number: 1062

Duplicate entry 'abc@abc' for key 2

INSERT INTO users (email, city) VALUES ('[email protected]', 'jeddah')

Filename: /home/content/f/a/h/fahadghafoor/html/fahad/models/users_model.php

Line Number: 12

Model file user_model.php

<?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' => $this->input->post('email'),
                'city' => $this->input->post('city'),                           
            );
            $insert = $this->db->insert('users', $new_member_insert_data);
            return $insert;


    }//create_member
}

Controller file user.php

<?php 

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('user_agent');
        $this->load->library('form_validation');
    }

    public function create_user() {
        // field name, error message, validation rules
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('city', 'City', 'trim|required');
        if ($this->form_validation->run() == FALSE) {
            if ($this->input->post("lang") == "en") {
                if ($this->agent->is_mobile()) {
                    $this->load->view('m_english_signup');
                } else {
                    $this->load->view('d_english_signup');
                }
            } //if ($this->agent->is_mobile())
            else {
                if ($this->agent->is_mobile()) {
                    $this->load->view('m_arabic_signup');
                } else {
                    $this->load->view('d_arabic_signup');
                }
            }
        } else {
            $this->load->model('Users_model');
            if ($query = $this->Users_model->create_member()) {
                if ($this->input->post("lang") == "en") {
                    if ($this->agent->is_mobile()) {
                        $this->load->view('m_english_thanks');
                    } else {
                        $this->load->view('d_english_thanks');
                    }
                } else {
                    if ($this->agent->is_mobile()) {
                        $this->load->view('m_arabic_thanks');
                    } else {
                        $this->load->view('d_arabic_thanks');
                    }
                }
            }
        }
    }

}
like image 904
FormaL Avatar asked Jan 15 '14 14:01

FormaL


Video Answer


2 Answers

It's very simple....Just change

$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

with is_unique rule

$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[TABLENAME.COLUMNNAME]');

TABLENAME - Your table name COLUMNNAME - Your relevant column name which contains Email.Ex:-email

like image 98
CodeCanyon Avatar answered Oct 04 '22 05:10

CodeCanyon


In ./application/config/database.php set $db['default']['db_debug'] = FALSE;

And then try to catch the error number in your Users_model.

$insert = $this->db->insert('users', $new_member_insert_data);
if (!$insert && $this->db->_error_number()==1062) {
   //some logics here, you may create some string here to alert user
} else {
   //other logics here
}
like image 44
danisupr4 Avatar answered Oct 04 '22 07:10

danisupr4