-3

注册新用户时如何显示用户存在以防止多次注册同名。这是我的模型代码。

class ModelUser extends CI_Model {

public function creatAccount()
{
    $userId =   $_POST['user_id'];
    $password   =   sha1($_POST['password']);

    $this->form_validation->set_rules('user_id', 'Userid',     

    trim|required|valid_userid|is_unique[account.user_id]|xss_clean');
    if ($this->form_valudation == false)
        return 'User already exist'
        }else
   {
     $this->db->query("INSERT INTO account(user_id,password)
    VALUES ('$userId','$password'')");
    }
4

1 回答 1

1

首先,您必须在构造函数中加载表单验证,

$this->load->library('form_validation');

现在在 creatAccount 函数中

public function creatAccount()
{
    $userId =   $this->input->post('user_id');
    $password   =   sha1( $this->input->post('password'));
    $this->form_validation->set_rules('user_id','Userid', 'trim|required|is_unique[tablename.coloumnname]|xss_clean');
     if($this->form_validation->run() == TRUE)
        {
    $this->db->query("INSERT INTO account(user_id,password)
    VALUES ('$userId','$password')");

            }else{
         return false;
}


    }
于 2013-08-08T04:58:06.370 回答