plutomed Posted July 12, 2009 Share Posted July 12, 2009 When I upload values into a database from a csv file, the values are added correctly except the date and time ones. They are added as the default. ??? I've tried to trim each of the values, but it still didn't work. Database: CREATE TABLE IF NOT EXISTS `entries` ( `id` int(11) NOT NULL auto_increment, `subject` varchar(50) NOT NULL default '', `location` varchar(50) NOT NULL default '', `description` text NOT NULL, `start_time` datetime NOT NULL default '0000-00-00 00:00:00', `end_time` datetime NOT NULL default '0000-00-00 00:00:00', `entry_type` varchar(11) NOT NULL default '', PRIMARY KEY (`id`) ) The code to upload and add it: if(($_FILES["csv_file"]["type"] == "application/vnd.ms-excel") && ($_FILES["csv_file"]["size"] < 2000000)) { if($_FILES["csv_file"]["error"] > 0) { echo "Error:".$_FILES["csv_file"]["error"]."<br />"; } else { $i=0; $val = array(); $handle = fopen($_FILES["csv_file"]["tmp_name"], "r"); while ((list($val[0],$val[1],$val[2],$val[3],$val[4],$val[5],$val[6],$val[7],$val[8],$val[9]) = fgetcsv($handle, 1000, ",")) != FALSE) { for($j=0;$j<10;$j++) { $val[$j] = explode("\"", $val[$j]); } $val[1][1] = explode("/",$val[1][1]); $val[3][1] = explode("/",$val[3][1]); $val[2][1] = explode(":",$val[2][1]); $val[4][1] = explode(":",$val[4][1]); if($i>0) { $start = $val[1][1][2]."-".check_zero($val[1][1][1])."-".check_zero($val[1][1][0])." ".$val[2][1][0].":".$val[2][1][1].":".$val[2][1][2]; $end = $val[3][1][2]."-".check_zero($val[3][1][1])."-".check_zero($val[3][1][0])." ".$val[4][1][0].":".$val[4][1][1].":".$val[4][1][2]; mysql_query("INSERT INTO entries(subject,location,description,start_time,end_time,entry_type) VALUES('".$val[0][1]."', '".$val[9][1]."', '".$val[8][1]."', '".$start."', '".$end."', 'Memo' )") or die(mysql_error()); } $i++; } fclose($handle); mysql_query("DELETE FROM entries WHERE subject=''"); } } The .csv file: "Off 31 jul - 8 aug","12/07/2009","14:42:38","12/07/2009","14:42:38","False","","","","" Link to comment https://forums.phpfreaks.com/topic/165713-weird-goings-ondatabase-and-csv/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 12, 2009 Share Posted July 12, 2009 Echo $start and $end to see what is actual in them. Link to comment https://forums.phpfreaks.com/topic/165713-weird-goings-ondatabase-and-csv/#findComment-874144 Share on other sites More sharing options...
plutomed Posted July 12, 2009 Author Share Posted July 12, 2009 The values that I wanted. 2009-07-12 14:42:38 2009-07-12 14:42:38 Link to comment https://forums.phpfreaks.com/topic/165713-weird-goings-ondatabase-and-csv/#findComment-874195 Share on other sites More sharing options...
plutomed Posted July 12, 2009 Author Share Posted July 12, 2009 Is there a way to print out all characters escaping everything? e.g. echo "\tHello World"; will write <a tab>Hello World. Is there a way to get it to print \tHello World. Link to comment https://forums.phpfreaks.com/topic/165713-weird-goings-ondatabase-and-csv/#findComment-874200 Share on other sites More sharing options...
plutomed Posted July 12, 2009 Author Share Posted July 12, 2009 Hmm. Must be some magical magic that is stopping it. Link to comment https://forums.phpfreaks.com/topic/165713-weird-goings-ondatabase-and-csv/#findComment-874257 Share on other sites More sharing options...
plutomed Posted July 13, 2009 Author Share Posted July 13, 2009 Ok, I got a way around it. Mush have been some white space in there that trim couldn't remove. ??? I used $val[1][1] = preg_split("//",$val[1][1]); and it worked. ??? Link to comment https://forums.phpfreaks.com/topic/165713-weird-goings-ondatabase-and-csv/#findComment-874290 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.