如果电子邮件已经存在,请不要在表格中输入数据并给出提示(已经存在!)

时间:2016-07-09 10:04:07

标签: php mysql

我在php中有一个简报形式,它插入电子邮件或者它已经预先存储在数据库表中,但我想如果电子邮件已经存在则它不会在数据库表中添加重复条目并提示(&# 34;已经存在")。请修改下面的代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Newsletter extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->model('stories_model');
    }
    public function index()
    {
        $this->load->view('header');
        $this->load->view('navigation');
        $this->load->view('pages/newsletter');
        $this->load->view('footer');
    }
    public function register()
    {
        $email = $this->input->post('email');

        $arr['result'] = 'confirm';
        $arr['message'] = '<ul>';


        if (strlen($email) == 0) {
            $arr['result'] = 'error';
            $arr['message'] .= '<li>Please fill the email.</li>';
        }

        if ($arr['result'] != 'error') 
        { 
            $insertcomment=$this->db->insert('newsletter_subscribers',array('email'=>$email));

            $arr['result'] = 'confirm';
            $arr['message'] = 'Successfull Added.';
        }
        echo json_encode($arr);
    }
    public function cronjob()
    {
        $data['stories'] = $this->newsletter_model->get_stories($email);
        $data['emails'] = $this->newsletter_model->get_emails_subscribers();
    }

}

2 个答案:

答案 0 :(得分:0)

您可以使用get_where

        if ($arr['result'] != 'error') 
        { 
            if(!$this->db->get_where('newsletter_subscribers', array('email' => $email), 0, 0)) {
            $insertcomment=$this->db->insert('newsletter_subscribers',array('email'=>$email));

            $arr['result'] = 'confirm';
            $arr['message'] = 'Successful Added.';

            } else {

            $arr['result'] = 'error';
            $arr['message'] = 'Already Added.';

            }

        }

答案 1 :(得分:0)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Newsletter extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->model('stories_model');
    }
    public function index()
    {
        $this->load->view('header');
        $this->load->view('navigation');
        $this->load->view('pages/newsletter');
        $this->load->view('footer');
    }
    public function register()
    {
        $email = $this->input->post('email');

        $arr['result'] = 'confirm';
        $arr['message'] = '<ul>';


        if (strlen($email) == 0) {
            $arr['result'] = 'error';
            $arr['message'] .= '<li>Please fill the email.</li>';
        }

        if ($arr['result'] != 'error') 
        { 
            $getEmails = $this->db->get_where('newsletter_subscribers', array('email' => $email)
            $countEmails = $getEmails->num_rows();
            if($countEmails < 1)
            {
                $insertcomment=$this->db->insert('newsletter_subscribers',array('email'=>$email));
                $arr['result'] = 'confirm';
                $arr['message'] = 'Successfull Added.';
            }
            else
            {
                $arr['result'] = 'error';
                $arr['message'] = 'Email Already Exist.';
            }

        }
        echo json_encode($arr);
    }
    public function cronjob()
    {
        $data['stories'] = $this->newsletter_model->get_stories($email);
        $data['emails'] = $this->newsletter_model->get_emails_subscribers();
    }

}