我只是一个简单的问题:
在下面的函数中我想if the username is taken return the error return false but if it doesn't exist return true and continue
我不认为我的T / F顺序正确。
public function username_check($username)
{
if($username == TRUE)
{
$this->form_validation->set_message('username_check','Sorry the username %s is already taken!');
return true;
}else{
return FALSE;
}
}
更新
我有toms代码工作正常但我现在有一个问题似乎回调不起作用,它只是继续将用户添加到数据库,即使它已经存在!
查看:
<h1><?php echo $companyName; echo nbs(1);?> - <?php echo $pageTitle; ?></h1>
<?php
if($success == TRUE) {
echo '<section id = "validation">Page Updated</section>';
}
?>
<p>Error: <?php echo validation_errors();?></p>
<div class="formContent">
<form action="createUser" method="post">
<fieldset class="control-group">
<label for="userName">User Name:</label><input type="text" id="userName" name="userName" value="<?php echo set_value('userName'); ?>" placeholder="User Name">
<label for="userPassword">User Password:</label><input type="password" id="userPassword" name="userPassword" placeholder="User Password">
<label for="userFirstName">First Name:</label><input type="text" id="userFirstName" name="userFirstName" value="<?php echo set_value('userFirstName'); ?>" placeholder="First Name">
<label for="userLastName">Last Name:</label><input type="text" id="userLastName" name="userLastName" value="<?php echo set_value('userLastName'); ?>" placeholder="Last Name">
<label for="userEmail">E-Mail:</label> <input type="text" id="userEmail" name="userEmail" value="<?php echo set_value('userEmail'); ?>" placeholder="Admin E-mail">
<label for="userGroup"> User Group:</label>
<select name="userGroup" id="userGroup">
<option value="select"<?=(set_value('userGroup')=='select')?' selected="selected" ':''?>>Please Select</option>
<option value="admin" <?=(set_value('userGroup')=='admin')?' selected="selected" ':''?>>Admin</option>
<option value="user" <?=(set_value('userGroup')=='user')?' selected="selected" ':''?>>User</option>
</select>
<label for="userActive"> User Active:</label>
<select name="userActive" id="userActive">
<option value="select" <?=(set_value('userActive')=='select')?' selected="selected" ':''?>>Please Select</option>
<option value="yes" <?=(set_value('userActive')=='yes')?' selected="selected" ':''?>>Yes</option>
<option value="no" <?=(set_value('userActive')=='no')?' selected="selected" ':''?>>No</option>
</select>
<div>
<button type="submit" class="btn-primary">Create</button>
</div>
</fieldset>
</form>
</div>
控制器:
public function index()
{
//Form Validation prep making sure its all clean
$this->form_validation->set_rules('userName', 'User Name', 'trim|required|callback_username_check|xss_clean');
$this->form_validation->set_rules('userPassword', 'User Password', 'trim|required|xss_clean|sha1');
$this->form_validation->set_rules('userFirstName', 'First Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('userLastName', 'Last Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('userEmail', 'E-Mail', 'trim|required|xss_clean');
$this->form_validation->set_rules('userGroup', 'User Group', 'trim|required|xss_clean');
$this->form_validation->set_rules('userActive', 'User Active', 'trim|required|xss_clean');
//If form validation fails load previous page with errors else do the job and insert data into db
if($this->form_validation->run() == FALSE)
{
$data['success'] = "";
}else{
$username = $this->input->post('userName');
$password = $this->input->post('userPassword');
$firstname = $this->input->post('userFirstName');
$lastname = $this->input->post('userLastName');
$email = $this->input->post('userEmail');
$group = $this->input->post('userGroup');
$active = $this->input->post('userActive');
$passwordHash = $this->encrypt->sha1($password); // Lets encrypt the password why sha1? MD5 is for tossers
// If the data is correct follow through with db insert
if($this->users_model->createUser($username,$passwordHash,$firstname,$lastname,$email,$group,$active))
{
$data['success'] = TRUE;
}
}
$data['companyName'] = $this->core_model->companyName();
$data['pageTitle'] = "Create User";
$this->load->view('admin/assets/header', $data);
$this->load->view('admin/createUser', $data);
$this->load->view('admin/assets/footer');
}
public function username_check($username)
{
if($this->users_model->username_taken($username))
{
$this->form_validation->set_message('username_check', 'Sorry the username %s is taken!');
return TRUE;
}else{
return FALSE;
}
}
型号:
function createUser($username = NULL ,$passwordHash = NULL ,$firstname = NULL ,$lastname = NULL ,$email = NULL,$group = NULL ,$active = NULL)
{
$data = array('userName' => $username, 'userFirstName' => $firstname, 'userLastName' => $lastname, 'userEmail' => $email, 'userPassword' => sha1($passwordHash), 'userGroup' => $group, 'userActive' => $active);
$this->db->insert('users',$data);
return TRUE;
}
function username_taken($username)
{
$this->db->select('userName');
$this->db->from('users');
$this->db->where('userName', $username);
$query = $this->db->get();
if($query->num_rows() > 0)
{
return TRUE;
}else{
return FALSE;
}
答案 0 :(得分:3)
您现在遇到的问题是因为您同时使用get('users')
和from('users')
。你应该使用:
$this->db->select('userName');
$this->db->from('users');
$this->db->where('userName', $username);
$query = $this->db->get();
或者:
$this->db->select('userName');
$this->db->where('userName', $username);
$query = $this->db->get('users');
更新:要使其正常运行,您的代码应如下所示:
控制器:
public function index()
{
$this->form_validation->set_rules('userName', 'User Name', 'trim|required|callback_username_check|xss_clean');
// Etc
}
public function username_check($username)
{
if($this->users_model->username_taken($username))
{
$this->form_validation->set_message('username_check', 'Sorry the username %s is taken!');
return FALSE;
}
else
{
return TRUE;
}
}
型号:
function username_taken($username)
{
$this->db->where('userName', $username);
return $this->db->count_all_results('users') > 0;
}