您好我有一些代码如下所示,它使用now()命令将date_time和date插入到mysql字段中。
然而,date_time字段更新但日期字段没有更新,我似乎无法找出原因?
我的mysql字段是日期时间和日期
INSERT INTO vistordetails1
(ipaddress, client_id, type, date_time, company_name, location, search_term, trafic_source, no_of_pages, date, country_code) VALUES('$ip_address', '$client_id', '$type', now(),'$fields[11]','$fields[6]', '$keyword', '$referer','1', now(), '$country_code')
表格结构
`id` int(11) NOT NULL AUTO_INCREMENT,
`client_id` int(11) NOT NULL,
`master_id` int(11) NOT NULL,
`type` tinyint(1) NOT NULL ,
`date_time` datetime NOT NULL,
`company_name` varchar(250) NOT NULL,
`location` varchar(250) NOT NULL,
`no_of_pages` int(11) NOT NULL,
`trafic_source` varchar(250) NOT NULL,
`search_term` varchar(250) NOT NULL,
`is_repeater` tinyint(1) NOT NULL,
`classification` int(11) NOT NULL,
`owner` int(11) NOT NULL,
`alert_for_repeat_visit` tinyint(1) NOT NULL DEFAULT '0',
`is_hot_list` enum('0','1') NOT NULL DEFAULT '0',
`ipaddress` varchar(50) NOT NULL,
`country_code` varchar(10) NOT NULL,
`date` date NOT NULL,
答案 0 :(得分:0)
尝试使用CURDATE()而不是NOW()作为日期字段。
$INSERT = "INSERT INTO vistordetails1
(ipaddress,
client_id,
type,
date_time,
company_name,
location,
search_term,
trafic_source,
no_of_pages,
date,
country_code)
VALUES(
'".mysql_real_escape_string(trim($ip_address))."',
'".mysql_real_escape_string(trim($client_id))."',
'".mysql_real_escape_string(trim($type))."',
now(),
'".mysql_real_escape_string(trim($fields[11]))."',
'".mysql_real_escape_string(trim($fields[6]))."',
'".mysql_real_escape_string(trim($keyword))."',
'".mysql_real_escape_string(trim($referer))."',
'".mysql_real_escape_string(trim(1))."',
CURDATE(),
'".mysql_real_escape_string(trim($country_code'))."')";
另外,正如之前的用户所提到的,你真的需要确保你的数据被转义,或者使用mysql prepare。
答案 1 :(得分:0)
如果您需要将PHP日期转换为有效的MySql日期,那么您唯一需要做的就是 确保格式正确。
date
的默认datetime
/ Mysql
格式为:yyyy-mm-dd
和yyy-mm-dd hh:mm:ss
这样的事情会起作用。
$phpDate = date('Y-m-d H:i:s', time());
<强>然而强>
因为您要创建新日期,为什么要使用PHP
?在MySql
中,您可以使用NOW()
函数包含当前日期。
$sql = "INSERT INTO xyz (a, b, c, datetime) VALUES ('a', 'b', 'c', NOW());"
<强>另外强>
您可以将default value
CURRENT_TIMESTAMP
应用于表格列。这意味着你甚至不需要通过任何日期
因为数据库会处理它。
即ALTER TABLE test CHANGE datecolumn datecolumn DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;