我在PHP 5.4中创建了一个在SQL Server 2008 r2中创建记录的代码。 PHP代码创建一个日期:
$date = date("Y-m-d");
其中:
'2015-06-23'
该日期用于对SQL服务器的查询:
$insert = 'INSERT INTO AdAttend (AdEnrollSchedID, AdEnrollID, Date, StartTime, Attend, Absent, unitType, Status, Comment, Excused, ModFlag, UserID, DateAdded, DateLstMod, AdClassAttendID, Type)
VALUES ('.$AdEnrollSchedID.','.$AdEnrollID.','.$date.',\'1899-12-30 07:00:00.000\','.$present.','.$absent.',\'M\',\'A\',\''.$comment.'\',0,\'A\','.$instructorID.
','.$date.','.$date.','.$AdClassAttendID.',\'A\')';
查询似乎工作正常。记录按原样插入。但是当我检查表中的记录时,日期是:
'1905-06-23 00:00:00.000'
所以问题是,为什么将2015年改为1905年?
答案 0 :(得分:2)
插入转义引号或在单引号中使用双引号
$insert = 'INSERT INTO AdAttend (AdEnrollSchedID, AdEnrollID, Date, StartTime, Attend, Absent, unitType, Status, Comment, Excused, ModFlag, UserID, DateAdded, DateLstMod, AdClassAttendID, Type)
VALUES ('.$AdEnrollSchedID.','.$AdEnrollID.',\''.$date.'\',\'1899-12-30 07:00:00.000\','.$present.','.$absent.',\'M\',\'A\',\''.$comment.'\',0,\'A\','.$instructorID.
','.$date.','.$date.','.$AdClassAttendID.',\'A\')';
OR
$insert = 'INSERT INTO AdAttend (AdEnrollSchedID, AdEnrollID, Date, StartTime, Attend, Absent, unitType, Status, Comment, Excused, ModFlag, UserID, DateAdded, DateLstMod, AdClassAttendID, Type)
VALUES ('.$AdEnrollSchedID.','.$AdEnrollID.',"'.$date.'",\'1899-12-30 07:00:00.000\','.$present.','.$absent.',\'M\',\'A\',\''.$comment.'\',0,\'A\','.$instructorID.
','.$date.','.$date.','.$AdClassAttendID.',\'A\')';