我正在使用 codeigniter 编写一些新闻通讯代码,但是在尝试将日期时间值输入数据库时,我不断收到“0000-00-00 00:00:00”作为日期时间值,有人能指出我吗方向正确
我检查过类似的主题,但似乎没有一个适合我
这是错误的代码
<?php
class Process extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function add_quotes($str)
{
return "'".$str."'";
}
function newsletter_user($log_data)
{
$this->load->helper('date');
date_default_timezone_set('Africa/Lagos'); # add your city to set local time zone
$now = date('Y-m-d H:i:s');
$query = $this->db->query('SELECT email FROM newsletter WHERE email ='. $this->add_quotes($log_data['email']));
if (!empty($query->result()))
{
$this->session->set_userdata('error', 'This email is already registered');
}
else {
$this->db->query('INSERT INTO newsletter (first_name, last_name, email, date_subscribe) VALUES ('.$this->add_quotes($log_data['firstname']).', '.$this->add_quotes($log_data['lastname']).', '.$this->add_quotes($log_data['email']).', '.$this->add_quotes($log_data[date('Y-m-d H:i:s')]).')');
$this->session->set_userdata('error', 'Registration successful! You would be notified once we launch');
}
//return $query;
}
}
?>
最佳答案
事实证明 $log_data 导致了这个问题
可以通过删除“$log_data”来解决,无论哪种方式;
$this->db->query('INSERT INTO newsletter (first_name, last_name, email, date_subscribe) VALUES ('.$this->add_quotes($log_data['firstname']).', '.$this->add_quotes($log_data['lastname']).', '.$this->add_quotes($log_data['email']).', '.$this->add_quotes($now).')');
或
$this->db->query('INSERT INTO newsletter (first_name, last_name, email, date_subscribe) VALUES ('.$this->add_quotes($log_data['firstname']).', '.$this->add_quotes($log_data['lastname']).', '.$this->add_quotes($log_data['email']).', '.$this->add_quotes(date('Y-m-d H:i:s')).')');
4天就犯了一个愚蠢的错误!
关于php - 如何在 Codeigniter 中使用日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55601754/