Click here to Skip to main content
15,916,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting problem in importing the .csv file.actually it is successfully imported but all field are come in only one column.
Here is the code:
On index.php
XML
<html>
<form action="load.php" method="post" enctype="multipart/form-data">
    <input  type="file" name="file" id="file" />
    <input type="submit" value="Load" name="load"/>
</body>
</form>
</html>

On load.php
PHP
function connect_db()
{
$con = mysql_connect("localhost","root","");
    if (!$con)
       {
                die('Could not connect: ' . mysql_error());
        }
  mysql_select_db("new_import", $con);
}
if (isset($_POST['load']))
{    
 
    echo "button on is pressed  <br />";
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Error: " . $_FILES["file"]["error"] .
"You have not selected a file or some other error <br />";
    }
    else
    {       
        $file_name=$_FILES["file"]["name"];
        echo $file_name."<br />";
        $file_type=$_FILES["file"]["type"];
        echo $file_type."<br />";
      //  if($file_type!='text/csv')
        //{
          //  echo "Please the input file should be a .csv file";
        //}
       // else
        //{  
            echo "its correct";
            $location="C:\\xampp\\mysql\\data\\import\\";
            move_uploaded_file($_FILES["file"]["tmp_name"],$location . $_FILES["file"]["name"]);
//}
            connect_db();
            
          /* $q= "LOAD DATA LOCAL INFILE '$file_name' INTO TABLE import1
    	FIELDS TERMINATED BY ';'
	   LINES TERMINATED BY '\n'
	   IGNORE 1 LINES
	   (@name, mobno)
	   SET name = @name,mobno=mobno";*/
            $q="LOAD DATA INFILE '$file_name' INTO TABLE import1
                FIELDS TERMINATED BY '|'
                
                LINES TERMINATED BY '\n'
                (@name,mobno)
                SET name=@name,mobno=@mobno";
mysql_query($q) or die(mysql_error());
}
}

?>

and problem is as follows in mysql database
name|mob.no
neha|9561061902

name|mob.no
neha|9561061902
Posted
Updated 5-Jul-12 23:30pm
v2

What is the separator used in csv. I think you need to split the string before saving to database.

Regards
Sebastian
 
Share this answer
 
Comments
project virus 6-Jul-12 5:36am    
Ya I thnk so but this not work...'|' seperator used in csv
project virus 6-Jul-12 5:39am    
how to split the string before saving to database
Sebastian T Xavier 6-Jul-12 5:51am    
using string.split
project virus 6-Jul-12 5:53am    
sorry i dnt knw how to use it
Sebastian T Xavier 6-Jul-12 5:54am    
just google it ....search for string.split using c#
hey dude i think for CSV file u should use "," or "~" on the "FIELDS TERMINATED BY" field any way try it and let me know ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900