如何在codeigniter中检查用户和电子邮件地址的可用性?

时间:2016-12-06 05:43:14

标签: php mysql codeigniter

我尝试使用我的MVC注册新用户,除了可用性检查之外都好,我想检查用户名和电子邮件地址是否已存在于数据库中,如果新注册不可用,则会出现错误。我是CodeIgniter的新手,需要帮助。 这是我的控制器

<?php 
class Add_new_user extends CI_Controller {
    function index(){
        $this->load->view('signup_view');// loading form view
    }

function insert_into_db(){
    $user = array(
            "fname" => $_POST["fname"],
            "lname" => $_POST["lname"],
            "email" => $_POST["email"],
            "username" => $_POST["username"],
            "gender" => $_POST["gender"],
            "phone" => $_POST["phone"],
            "address" => $_POST["address"],
            );

    $this->load->model('signup_model');
    $result=$this->signup_model->insert_into_db($user);     


    if($result==1){
        $this->load->view('thanks_page');
    }
   }
  }
?>

这是我的模特

<?php 

class Signup_model extends CI_Model{

    public function __construct() {
      parent::__construct();
      }
    function insert_into_db($data)
    {
            if( 

            $result=$this->db->insert('gymers', $data) ) {
                return $result;
            } 
            else {
                echo "ERROR";
            }
        }
   }
?>

请帮忙。谢谢。

2 个答案:

答案 0 :(得分:0)

您应该使用Codeigniter的表单验证

在控制器中

public function index()
{
  $this->load->helper(array('form','url'));
  $this->load->library(array('session', 'form_validation', 'email'));

  $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|is_unique[db_table.col_email]');
  // other rule if any.

  //validate form input
  if ($this->form_validation->run() == FALSE)
  { 
    $this->load->view('signup_view');// loading form view
  } else { 
    // Form success
    // insert to db.
  }
}

查看

<?php 
  $attributes = array("class" => "contact-form2 con_center2", "name" => "contactform", "id" => "contact-form2");
  echo form_open("Add_new_user/index", $attributes); ?>

  <!-- Your inputs are here. -->
    <input type="text" name="email" placeholder="Email*" value="<?php echo set_value('email'); ?>" required/>
    <span class="text-danger"><?php echo form_error('email'); ?></span>

<?php echo form_close(); ?>

请同时参考this作为教程

答案 1 :(得分:0)

<?php 
class Add_new_user extends CI_Controller 
{
    function Add_new_user() {
       parent ::__construct();
        $this->load->helper('url');
        $this->load->library('session');
    }
    function index(){
        $this->load->view('signup_view');// loading form view
    }

function insert_into_db(){
    $user = array(
            "fname" => $_POST["fname"],
            "lname" => $_POST["lname"],
            "email" => $_POST["email"],
            "username" => $_POST["username"],
            "gender" => $_POST["gender"],
            "phone" => $_POST["phone"],
            "address" => $_POST["address"],
            );
    $user_chk_data['email'] = $_POST["email"];
    $user_chk_data['username'] = $_POST["username"];
    $this->load->model('signup_model');
    $user_data = $this->signup_model->check_username_email($user_chk_data);

    if(!empty($user_data))
    { 
       $this->session->set_flashdata('error_msg', 'User already registered.');
        redirect('add_new_user');
    }
    else
    {
      $result=$this->signup_model->insert_into_db($user); 
      $this->load->view('thanks_page');
    }    
   }
  }
?>

您的模型文件

<?php 

 class Signup_model extends CI_Model{

public function __construct() {
  parent::__construct();
  }
function insert_into_db($data)
{
        if($result=$this->db->insert('gymers', $data))
        {
            return TRUE
        } 
        else {
            return FALSE
        }
    }
function check_username_email($user_data)
{

    $this->db->select("*");
    $this->db->from("gymers");
    $this->db->where("email",$user_data['email']);
    $this->db->where("username",$user_data['username']);

    $query_data = $this->db->get()->row_array();
    return $query_data;
}

 }
?>

并检查signup_view文件中的flashdata消息

<?php if($this->session->flashdata('error_msg') != ''){?>
        <span><?php echo $this->session->flashdata('error_msg');?></span>   
<?php } ?>

试试吧...... / .... /