在下面的代码中我放置了控制器和型号我插入名字,姓氏,电子邮件地址,用户名,密码和日期。但是我无法将当前日期插入mysql请帮助解决问题。
控制器:登录
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');
}
答案 0 :(得分:0)
我认为这个字符串中存在问题:
'date' => $this->input->post('date','curdate()')
您的框架(它的框架?)尝试从post插入一个值。如果此值为空,则插入curdate()。在MySQL NOW()
中将插入当前日期或日期时间。
答案 1 :(得分:0)
首先,在您输入日期的模型方法中,加载“日期”帮助器:
$this->load->helper('date');
或者,如果您在多个方法中使用它,请继续将其加载到类构造函数中。
然后,插入当前日期:
'date' => date('Y-m-d H:i:s', now())
那会让你顺利上路!
答案 2 :(得分:0)
你的语法中有一个错误:
'date' => $this->input->post('date','curdate()')
应该是
'date' => $this->input->post($this->curdate())
或者,为什么不做呢
'date' => date('Y-m-d H:i:s')