Jump to content

Weird goings on...Database and .csv


plutomed

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.