簡體   English   中英

將當前日期插入mysql

[英]To insert current date into mysql

在下面的代碼中,我使用了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');           
            }
        }

    }

型號Membership_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;
    }

您可以將其交給您的一個助手:

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:創建新的助手?

注意:在這里,我的任務是為網站定義默認時區。 您可以根據需要使用UTC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM