无法通过php脚本将csv数据插入mysql表

时间:2017-01-24 08:02:30

标签: php mysql csv datetime

我正在尝试将csv数据插入到mysql表中。它适用于除startdate,enddate,slotmsg列以外的所有列。附上了csv文件。

create table jobtktdesc(
    jobtktnum int(6) not null,
    custname varchar(50) not null,
    sitename varchar(50) not null,
    address varchar(50) not null,
    typecont varchar(30) not null,
    idcontract int(7) not null,
    mainttype varchar(40) not null,
    empname varchar(40) not null,
    empmail varchar(60) not null,
    starttime datetime not null,
    endtime datetime not null,
    slotmsg varchar(100)
);

然后在文件中我使用fgetcsv作为csv文件,并使用以下脚本插入每一行。

<?php

$starttime = new DateTime($getData[16]);
$endtime   = new DateTime($getData[17]);

$sql    = "INSERT into jobtktdesc (jobtktnum,custname,sitename,typecont,idcontract,address,empname,empmail,mainttype,starttime,endtime,slotmsg) 
                            values ('" . $getData[0] . "','" . $getData[1] . "','" . $getData[2] . "','" . $getData[3] . "','" . $getData[4] . "','" . $getData[5] . "','" . $getData[13] . "','" . $getData[14] . "','" . $getData[15] . "','" . $starttime->format('Y-m-d H:i') . "','" . $endtime->format('Y-m-d H:i') . "','" . substr($getData[18], 0, 95) . "')";
$result = mysqli_query($mysqli, $sql);

?>

csv文件包含以下数据。

ScheduleID,CustName,SiteName,Description,AgrmntID,timecreate,Address1,Address2,Address3,City,Phone,Contact,TechList,EmpName,Email,RepairCode,StartTime,EndTime,SlotMessage
57954,XXXX,EEEE,llk,404,1/24/17,street1,,,antartica,,,144,Paul,john@gmail.com,do something,1/25/17 8:00,1/25/17 17:30,
57953,YYYY,DDDD,lkkjdf,404,1/24/17,street2,,,antartica,,,118,Smith,paul@gmail.com,do something,1/25/17 8:00,1/25/17 17:30,
57952,ZZZZ,CCCC,adsdfs,559,1/23/17,street3,,,antartica,,john,142,john,john@gmail.com,do something,1/24/17 15:00,1/24/17 17:30,Inspect
57951,AAAA,BBBB,ssfss,559,1/23/17,street4,,,antartica,,jane,20,jane,jane@gmail.com,do something,1/24/17 15:00,1/24/17 17:30,Inspect

我无法理解为什么会这样。

此致

1 个答案:

答案 0 :(得分:1)

解决了它。日期时间设置不正确