值未在codeigniter tank auth中保存在数据库中

时间:2014-02-06 20:13:17

标签: php codeigniter tankauth

我在tank auth.Field的注册表单中添加了两个字段,名称为fname和lname。 我在form_validation中添加了相同的内容,因为你可以看到控制器文件。但是用户名,密码,电子邮件被注入数据库期望fname和lname。代码中有什么问题?我检查了字段的名称,它们是表格验证也正常,但数据库字段名称也是fname和lname。有人可以解决问题。已经坐了很长时间但是找不到它们

auth.php控制器是

if ($use_username) {
                $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|max_length['.$this->config->item('username_max_length', 'tank_auth').']|alpha_dash');
            }
            $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['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
            $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]');
            $this->form_validation->set_rules('fname', 'First Name', 'trim|required|xss_clean'); // I added
            $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|xss_clean'); //I added


            $captcha_registration   = $this->config->item('captcha_registration', 'tank_auth');
            $use_recaptcha          = $this->config->item('use_recaptcha', 'tank_auth');
            if ($captcha_registration) {
                if ($use_recaptcha) {
                    $this->form_validation->set_rules('recaptcha_response_field', 'Confirmation Code', 'trim|xss_clean|required|callback__check_recaptcha');
                } else {
                    $this->form_validation->set_rules('captcha', 'Confirmation Code', 'trim|xss_clean|required|callback__check_captcha');
                }
            }
            $data['errors'] = array();

            $email_activation = $this->config->item('email_activation', 'tank_auth');

            if ($this->form_validation->run()) {                                // validation ok
                if (!is_null($data = $this->tank_auth->create_user(
                        $use_username ? $this->form_validation->set_value('username') : '',
                        $this->form_validation->set_value('fname'),//I added
                        $this->form_validation->set_value('lname'),
                        $this->form_validation->set_value('email'),
                        $this->form_validation->set_value('password'),
                        $email_activation)))

user.php模型

function create_user($data, $activated = TRUE)
    {
        $data['created'] = date('Y-m-d H:i:s');
        $data['activated'] = $activated ? 1 : 0;

        if ($this->db->insert($this->table_name, $data)) {
            $user_id = $this->db->insert_id();
            if ($activated) $this->create_profile($user_id);
            return array('user_id' => $user_id);
        }
        return NULL;
    }

1 个答案:

答案 0 :(得分:0)

你好我再次通过不编辑TA模型来完成这项工作。 我做了什么让TA完成工作,在create_user()完成后我立即对这个新创建的用户进行了更新,其中包含我需要的其他信息(名字,姓氏,性别......)

所以,打开

<强> /libraries/Tank_auth.php

并添加此方法

/**
 * Update user information on site (user_profile)
 * make sure keys of array match table fields to update
 * @param   integer
 * @param   array
 * @return  NULL
 */

function update_profile($user_id, $data)
{
    $this->ci->users->update_profile($user_id, $data);
    return NULL;
}

滚动至function create_user(...)添加为最后一个参数$user_data,因此功能声明看起来像这样

function create_user($username, $email, $password, $email_activation, $user_data) {...}

并编辑一些这样的代码(在create_user方法中)

if (!is_null($res = $this->ci->users->create_user($data, !$email_activation))) {
        $data['user_id'] = $res['user_id'];
        $data['password'] = $password;
        unset($data['last_ip']);
        $this->update_profile($data['user_id'], $user_data); //this line is important!
        return $data;
}

现在保存此文件并转到 models / tank_auth / users.php ,创建此方法

/**
 * Update profile for a user
 *
 * @param   int
 * @param   array
 * @return  bool
 */
function update_profile($user_id, $data)
{

    $this->db->where('id', $user_id);
    $this->db->update($this->profile_table_name, $data);
}

现在转到 controllers / auth.php

找到register()方法

并搜索(这是您在此处发布的代码的底部部分)

if ($this->form_validation->run()) {                                // validation ok
                if (!is_null($data = $this->tank_auth->create_user(
                        $use_username ? $this->form_validation->set_value('use...

并更改它,使其看起来像这样

if ($this->form_validation->run()) {                                
                $user_data = array(
                    'first_name'    => set_value('fname'),
                    'last_name' => set_value('lname'),
                    ); // <- IMPORTANT
                if (!is_null($this->data = $this->tank_auth->create_user(
                        $use_username ? $this->form_validation->set_value('username') : '',
                        $this->form_validation->set_value('email'),
                        $this->form_validation->set_value('password'),
                        $email_activation, $user_data))) {                                  

                    $this->data['site_name'] = $this->config->item('website_name', 'tank_auth');

                    if ($email_activation) {...