创建注册时我正在关注http://www.youtube.com/watch?v=ua1pWx8WjFM。很长一段时间,我试图找到为什么我可以注册任何数量的相同用户名,而代码和其他一切看起来相同,并在视频中正常工作。
我的模型“user_model”:
function register_user($username, $password, $name, $email)
{
$sha1_password = sha1($password);
$query_str = "INSERT INTO registration (username, password, name, email) VALUES (?, ?, ?, ?)";
$this->db->query($query_str, array($username, $sha1_password, $name, $email));
}
function username_check($username)
{
$query_str = "SELECT username from registration where username = ?";
$result = $this->db->query($query_str, $username);
if ($result->num_rows() > 0)
{
//username exists
return true;
}
else
{
//username doesn't exist
return false;
}
}
}
controller“user.php”:
function User()
{
parent::__construct();
$this->load->model('User_model');
}
function index()
{
//$this->load->model('User_model');
$this->register();
}
function register()
{
//$this->load->model('User_model');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]|xss_clean|callback_username_not_exists');
$this->form_validation->set_rules('name', 'Name', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|min_length[6]|xss_clean|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
$this->form_validation->set_rules('password_conf', 'Password Confirmation', 'trim|required|alpha_numeric|min_length[6]|matches[password]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
//$this->load->model('User_model');
$this->view_data['page_title'] = 'Welcome to a Tutorial';
$this->view_data['page_content'] = 'view_login_form';
$this->load->view('view_register', $this->view_data); //errors
}
else
{
$username = $this->input->post('username');
$name = $this->input->post('name');
$password = $this->input->post('password');
$email = $this->input->post('email');
// $this->load->model('User_model');
$this->User_model->register_user($username, $password, $name, $email);
}
function username_check($username)
{
$this->form_validation->set_message('username_check', 'That %s already exists');
if ($this->User_model->check_username($username))
{
return false;
}
else
{
return true;
}
}
} }
那么,这里有什么问题?
答案 0 :(得分:2)
真的非常明显;可能是视频中的疏忽。
您已为username
字段设置此验证规则:
$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]|xss_clean|callback_username_not_exists');
回调规则为callback_username_not_exists
您的回调函数称为username_check
。由于名称不匹配,它永远不会被调用。