检查用户标识的有效性

时间:2013-08-08 04:44:28

标签: php codeigniter

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

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'')");
    }

1 个答案:

答案 0 :(得分: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;
}


    }