2012-08-15 62 views
1

这里是我的功能,并在娄代码的日期为什么getdate不存储在数据库中?

$data->datecreated = getdate(); 

不会得到保存在数据库中。为什么? 注意,一条记录被插入,但dateCreated会现场貌似是忽略

public function generateCode($characters) { 

     //Generate the code 
     $possible = '23456789bcdfghjkmnpqrstvwxyz'; 
     $code = ''; 
     $i = 0; 

     while ($i < $characters) { 
      $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1); 
      $i++; 
     } 
     // Get the curent session id of the user   
     $session = JFactory::getSession(); 
     $session_id = $session->getId();   

     //Create an stdClass 
     $data =new stdClass(); 
     $data->sessionid = (integer)$session_id; 
     $data->captchacode = (string)$code;  
     $data->datecreated = getdate(); 

     $db = JFactory::getDBO(); 

     $query = $db->getQuery(true); 
     $query->delete($db->nameQuote('#__captchasessions')); 
     $query->where($db->nameQuote('sessionid').'='.$db->quote($session_id)); 
     $db->setQuery($query); 
     $db->query(); 

     $db->insertObject('#__captchasessions', $data, id);  

     return $code; 
    } 

这是表

delimiter $$ 

CREATE TABLE `ok6ut_captchasessions` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `sessionid` varchar(200) DEFAULT NULL, 
    `captchacode` varchar(10) DEFAULT NULL, 
    `datecreated` datetime DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8$$ 
+1

告诉我们发生了什么和在哪里。 PHP错误? MySQL错误? – Jocelyn 2012-08-15 23:18:54

+0

没有错误,它只是忽略条目,但其余的被插入 – themis 2012-08-15 23:23:41

+1

你不需要使用'$ db = JFactory :: getDBO();'两次 – Lodder 2012-08-15 23:25:27

回答

3

getdate()的结构在它的当前日期的细节返回一个数组,如果没有时间戳作为参数传入。

我希望你的数据库需要传入一个格式化的日期,而不是一个数组 - 如果你可以添加你的数据库结构,这可能会有所帮助。

编辑补充:

对于时间字段,我想你需要的东西,如:

$data->datecreated = date("Y-m-d H:i:s"); 
+0

是'datecreated'日期时间。那么如何通过时间戳呢? – themis 2012-08-15 23:22:52

+1

@themhz - 编辑建议 – andrewsi 2012-08-15 23:28:59

+0

是的,它的作品,你摇滚,thanx! – themis 2012-08-15 23:33:12

相关问题