我很长时间都在寻找答案。我是PHP的新手,我正在尝试将当前日期和时间插入到datetime类型的数据库表列中。但它给了我错误。
$tsql ="INSERT INTO metaTagsTable (Email,nMetaTheme,nMetaSpecification,sMetaDuration,nChildrenYearTo,nChildrenYearFrom,nMetaPaxChildren,nMetaPax,dMetaReturnDate,dMetaDepartureDate, nMetaDestinationSubArea,nMetaDestinationArea,nMetaDestinationCountry,nMetaDestination,nMetaDepartureSubArea,nMetaDepartureArea,nMetaDepartureCountry,nMetaDeparture,sMetaCategory,ModifiedDate,dCreatedDate,nSupplierId,VisitorID) VALUES ('".$array[0]."','".$array[1]."','".$array[2]."','".$array[3]."','".$array[4]."','".$array[5]."','".$array[6]."','".$array[7]."',
'".$array[8]."','".$array[9]."','".$array[10]."','".$array[11]."','".$array[12]."','".$array[13]."','".$array[14]."','".$array[15]."','".$array[16]."','".$array[17]."','".$array[18]."',getdate(),getdate(),'".$array[21]."','".$array[22]."')";
数组来自此输入:
test;null;null;null;null;null;null;null;null;null;null;null;"499366";null;null;null;null;null;"Catalogue";null;null;null;null
错误:
Array ( [0] => Array ( [0] => 22007 [SQLSTATE] => 22007 [1] => 241 [code] => 241 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Conversion failed when converting date and/or time from character string. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Conversion failed when converting date and/or time from character string. ) )
表设计:
CREATE TABLE [dbo].[metaTagsTableTMP](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Email] [nvarchar](50) NULL,
[nMetaTheme] [nvarchar](20) NULL,
[nMetaSpecification] [nvarchar](20) NULL,
[sMetaDuration] [nvarchar](20) NULL,
[nChildrenYearTo] [nvarchar](20) NULL,
[nChildrenYearFrom] [nvarchar](20) NULL,
[nMetaPaxChildren] [nvarchar](20) NULL,
[nMetaPax] [nvarchar](20) NULL,
[dMetaReturnDate] [nvarchar](50) NULL,
[dMetaDepartureDate] [nvarchar](50) NULL,
[nMetaDestinationSubArea] [nvarchar](20) NULL,
[nMetaDestinationArea] [nvarchar](20) NULL,
[nMetaDestinationCountry] [nvarchar](20) NULL,
[nMetaDestination] [nvarchar](20) NULL,
[nMetaDepartureSubArea] [nvarchar](20) NULL,
[nMetaDepartureArea] [nvarchar](20) NULL,
[nMetaDepartureCountry] [nvarchar](20) NULL,
[nMetaDeparture] [nvarchar](20) NULL,
[sMetaCategory] [nvarchar](20) NULL,
[ModifiedDate] [datetime] NULL,
[dCreatedDate] [datetime] NULL,
[nSupplierId] [nvarchar](20) NULL,
[VisitorID] [nvarchar](50) NULL,
CONSTRAINT [PK_metaTagsTableTMP] PRIMARY KEY CLUSTERED
答案 0 :(得分:0)
如果是一个字符串::
,则SqlDateTime需要使用以下格式的dateTime参数yyyy-MM-dd HH:mm:ss
答案 1 :(得分:0)
我没有想到getdate()
作为一种方法,我不知道为什么。所以我没有提出正确的引用。
这是正确的查询:
$tsql ="INSERT INTO metaTagsTable (Email,nMetaTheme,nMetaSpecification,sMetaDuration,nChildrenYearTo,nChildrenYearFrom,nMetaPaxChildren,nMetaPax,dMetaReturnDate,dMetaDepartureDate, nMetaDestinationSubArea,nMetaDestinationArea,nMetaDestinationCountry,nMetaDestination,nMetaDepartureSubArea,nMetaDepartureArea,nMetaDepartureCountry,nMetaDeparture,sMetaCategory,ModifiedDate,dCreatedDate,nSupplierId,VisitorID) VALUES ('".$array[0]."','".$array[1]."','".$array[2]."','".$array[3]."','".$array[4]."','".$array[5]."','".$array[6]."','".$array[7]."','".$array[8]."','".$array[9]."','".$array[10]."','".$array[11]."','".$array[12]."','".$array[13]."','".$array[14]."','".$array[15]."','".$array[16]."','".$array[17]."','".$array[18]."',".getdate().",".getdate().",'".$array[21]."','".$array[22]."')";