php - 如何在 Codeigniter 中使用日期时间?

标签 php mysql codeigniter

我正在使用 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/

相关文章:

php - 我怎样才能找到我的输出开始的地方?

php - 如何将 opencart 与亚马逊 CDN 集成?

mysql - 如何编写查询以获得特价(如果有)

php - 从 codeigniter url 中删除 index.php

codeigniter - 为什么 Flashdata 值在重定向到 Codeigniter 中的另一个函数时变为空值?

php - mysql_fetch_array 中给出的字符串

php - 将单行注释转换为 block 注释

python - django-storages dropbox.stone_validators.ValidationError

php - mysql查询检索过去15天每天的记录数

php - 在 CodeIgniter 中收到 "Disallowed Key Characters"错误