View Single Post
Join Date: Dec 2006
Posts: 34
Reputation: php_coder is an unknown quantity at this point 
Solved Threads: 0
php_coder php_coder is offline Offline
Light Poster

Re: insert csv file into mysql through php

 
0
  #4
Dec 15th, 2006
Originally Posted by zippee View Post
Before you can 'split' the cvs data, you need the following:[php]
// after uploading using a html form
$tmpName = $_FILES['userfile']['tmp_name'];
$fp = fopen($tmpName, 'r');
$fr=fread($fp, filesize($tmpName));
$line = explode("\n", $fr);
// etc etc ...
[/php]
<?
if($_REQUEST['submit'])
{
$contents = file ('./email_entries.csv');
for($i=0; $i<sizeof($contents); $i++)
{
$line = trim($contents[$i],'",');
$arr = explode(',', $line);
// $sql = "insert into employees values ('".implode("','", $arr)."')";
$sql = mysql_query("insert into employees(first_name,last_name,email_id) values ('".$arr[0]."','".$arr[1]."','".$arr[2]."')");
}
}
Reply With Quote