Arjun Bhandari
Arjun Bhandari

Reputation: 299

Unable to insert csv data into mysql table through php script

I am trying to insert a csv data into mysql table. It works fine for all columns other than the startdate, enddate, slotmsg columns. The csv file is attached.

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)
);

then in the file i use fgetcsv for the csv file and insert each row using the following script.

<?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);

?>

The csv file has the following data.

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,[email protected],do something,1/25/17 8:00,1/25/17 17:30,
57953,YYYY,DDDD,lkkjdf,404,1/24/17,street2,,,antartica,,,118,Smith,[email protected],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,[email protected],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,[email protected],do something,1/24/17 15:00,1/24/17 17:30,Inspect

I am unable to understand why this is the case.

Regards

Upvotes: 0

Views: 55

Answers (1)

Arjun Bhandari
Arjun Bhandari

Reputation: 299

solved it. the datetime was being set incorrectly

Upvotes: 1

Related Questions