未返回所需的错误/成功消息


Not returning desired error/success message

由于某种原因,当我提交表单时,返回的内容是"创建用户时出现问题!请重试!"但它仍然会生成一个新的数据库条目,我不确定原因。我正在表单上运行测试,但根据我提交的数据,它应该会给我一个成功的消息。有什么想法吗?

编辑:我已经试了两天了,但似乎还是想不通。

控制器:

function register_submit()
{
    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric|strtolower');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric');
    $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]|min_length[6]|max_length[12]|alpha_numeric');
    $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|xss_clean|alpha');  
    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|xss_clean|alpha');    
    if (!$this->form_validation->run()) 
    {
        echo json_encode(array('error' => 'yes', 'message' => 'There was a problem submitting the form! Please refresh the window and try again!'));    
    }
    else
    {                           
        $user_data = $this->kow_auth->create_user($this->input->post('username'), $this->input->post('email'), $this->input->post('password'), $this->input->post('first_name'), $this->input->post('last_name'));
        if ($user_data == FALSE)      
        {
            echo json_encode(array('error' => 'yes', 'message' => 'There was a problem creating the user! Please try again!'));
        }     
        else
        {
            $data['activation_period'] = 48;
            $this->kow_auth->send_email('activate', $data['email'], $data);
            unset($data['password']);   
            echo json_encode(array('sucess' => 'yes', 'message' => 'You have successfully registered. Check your email address to activate your account!'));
        }
    }
}

库:

/**
 * Create new user on the site and return some data about it:
 * user_id, username, password, email, new_email_key (if any).
 *
 * @param   string
 * @param   string
 * @param   string
 * @param   string
 * @param   string
 * @return  array
 */
function create_user($username, $email, $password, $first_name, $last_name)
{
    if ((strlen($username) > 0) AND !$this->ci->users->is_username_available($username)) 
    {
        return FALSE;
    } 
    elseif (!$this->ci->users->is_email_available($email)) 
    {
        return FALSE;
    } 
    else 
    {
        $genPassHash = $this->ci->genfunc->GenPassHash($password);
        $max_user_id = $this->ci->users->get_max_users_id();
        $data = array(
            'username'      => $username,
            'password'      => $genPassHash[0],
            'password2'     => $genPassHash[1],
            'email'         => $email,
            'first_name'    => $first_name,
            'last_name'     => $first_name,
            'new_email_key' => md5(rand().microtime()),
            'user_id'       => $max_user_id,
            'ip_address'    => $this->ci->input->ip_address()
        );
        if (($result = $this->ci->users->create_user($data)) == FALSE)
        {
            $data['user_id'] = $res['user_id'];
            $data['password'] = $password;
            unset($data['last_ip']);
            return $data;
        }
    }
    return FALSE;
}

用户模型:

/**
 * Create new user record
 *
 * @param   array
 * @return  bool
 */
function create_user($data)
{
    $data['date_created'] = date('Y-m-d H:i:s');
    $this->db->set('username', $data['username']); 
    $this->db->set('password', $data['password']); 
    $this->db->set('password2', $data['password2']); 
    $this->db->set('email', $data['email']);
    $this->db->set('first_name', $data['first_name']);  
    $this->db->set('last_name', $data['last_name']); 
    $this->db->set('ip_address', $data['ip_address']); 
    $this->db->set('date_created', $data['date_created']); 
    $query = $this->db->insert('users');
    if ($query) 
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
} 

我认为在所有这些插入中的某个时刻可能会出现一些sql错误,并且$query为FALSE,您应该阅读有关事务的信息以避免这种部分插入。。。它主要是关于逻辑的。基本上,如果逻辑中的任何插入都将失败,那么您不想执行任何插入,所以请阅读有关事务的信息。你没有指定你使用的数据库平台,所以我不能给出一个确定的链接。。。如果您使用mysql,这里有一个链接:http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-transactions.html