How To Store Data From Csv File To Multiple Tables

Hiii alll,

      I have use this wiki [url="http://www.yiiframework.com/wiki/336/importing-csv-file-to-mysql-table-using-load-data-command/"]Import csv file[/url] to save data from csv file to my database.

My question is i have some columns in csv file that i want to store in another table in my database.

Suppose i have csv file with columns(EmpID,EmpName,EmpPhone,email) etc. And

I have two table tb1,tb2.

I want to store EmpID & EmpName from csv file to tb1 AND EmpPhone & email to tb2.




$sql="LOAD DATA LOCAL INFILE '".addslashes($tempLoc)."' INTO TABLE `tb1` FIELDS

			            TERMINATED BY ','

			            ENCLOSED BY '\"'

					        LINES

					            TERMINATED BY '\r\n'

					         IGNORE 1 LINES

					        (`EmpID`,`EmpName`)

					        ";


$sql1="LOAD DATA LOCAL INFILE '".addslashes($tempLoc)."' INTO TABLE `tb2` FIELDS

			            TERMINATED BY ','

			            ENCLOSED BY '\"'

					        LINES

					            TERMINATED BY '\r\n'

					         IGNORE 1 LINES

					        (`EmpPhone`,`email`)

					        ";




But it only shows 0 values in both columns in my database.

Not sure why you are getting only 0, but I think you may need to a different approach because of the table splitting of the information.

In pseudo-code:

  • open the file

  • read in first line, skip

then while no EOF

  • read in the next line

  • use explode()

  • create model1

  • create model2

  • assign values as needed

  • save each model

  • repeat.