Codeigniter - 模型 - 重复录入警报

For*_*maL 3 php mysql codeigniter

做了一个简单的订阅表单提交电子邮件和城市唯一的电子邮件ID我设置唯一,但如果电子邮件ID已经在数据库中它显示错误我需要在这里添加警报,如果已经在数据库中的数据提醒用户,但没有显示这样的完整细节.

发生数据库错误

错误号码:1062

键2的重复条目'abc @ abc'

INSERT INTO users(email,city)VALUES('abdullah @ gaya.com','jeddah')

文件名:/home/content/f/a/h/fahadghafoor/html/fahad/models/users_model.php

行号:12

模型文件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
}
Run Code Online (Sandbox Code Playgroud)

控制器文件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');
                    }
                }
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

Cod*_*yon 13

这很简单....只是改变

$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
Run Code Online (Sandbox Code Playgroud)

与is_unique规则

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

TABLENAME - 您的表名COLUMNNAME - 您的相关列名,其中包含Email.Ex:-email


dan*_*pr4 6

在 ./application/config/database.php 中设置 $db['default']['db_debug'] = FALSE;

然后尝试在您的 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
}
Run Code Online (Sandbox Code Playgroud)