0

在下面的代码中,我使用了 codeigniter 代码,我可以插入用户名、密码、名字、姓氏和电子邮件地址,但我无法插入当前日期,请帮助纠正这个问题。

控制器:登录

function create_member()
    {
        $this->load->library('form_validation');

        // field name, error message, validation rules
        $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');


        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('signup_form');
        }

        else
        {           
            $this->load->model('membership_model');

            if($query = $this->membership_model->create_member())
            {
                $data['main_content'] = 'signup_successful';
                $this->load->view('includes/template', $data);
            }
            else
            {
                $this->load->view('signup_form');           
            }
        }

    }

模型成员资格_model

*

function create_member()
    {

        $new_member_insert_data = array(
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'email_address' => $this->input->post('email_address'),         
            'username' => $this->input->post('username'),
            'date' => $this->input->post('date','curdate()'),
            'password' => md5($this->input->post('password'))                       
        );

        $insert = $this->db->insert('membership', $new_member_insert_data);
        if ($this->db->_error_number() == 1062)
                {
                echo"<script>alert('This value already exists');</script>";
                }
                if ($this->db->_error_number() == "")
                {
                $this->session->set_flashdata('create', 'create');
                }

        return $insert;
    }
4

1 回答 1

1

你可以把它交给你的一位助手:

function curdate() {
    // gets current timestamp
    date_default_timezone_set('Asia/Manila'); // What timezone you want to be the default value for your current date.
    return date('Y-m-d H:i:s');
}

并像这样使用它:

'date' => $this->input->post('date',curdate()), // curdate() as a function

如果你想在全局范围内使用这种东西,你可以把它放在一个自定义助手上并自动加载它。参考他的链接CodeIgniter: Create new helper?

注意:就我而言,我的任务是为网站定义默认时区。如果需要,您可以使用 UTC

于 2013-11-11T09:05:06.167 回答