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

时间:2012-01-24 22:04:48

标签: php codeigniter

出于某种原因,当我提交表单时,提交的内容是“创建用户时出现问题!请再试一次!”但它仍然是一个新的数据库条目,我不知道为什么。我在我的表单上运行测试,但是我提交的数据应该给我一个成功的消息。有什么想法吗?

编辑:我现在已经尝试了2天,但似乎仍无法解决这个问题。

控制器:

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;
    }
} 

1 个答案:

答案 0 :(得分:0)

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