Good Morning,

I have a CSV file with thousands of rows of data, and I want to have this data uploaded into a table in a MySQL database. I also want an area where the file could be selected from the computer for the data to be uploaded.

Could this be done?
If it could, kindly guide me through getting it done...

May

Recommended Answers

All 12 Replies

Have you tried phpmyadmin?

Hi All,

To Daedal...no, I haven't tried phpmyadmin...I would look into that now...

To Pritaeas...I would also now look at the website you have given to me...

To All...I will keep you all posted on my progress...

Thank much in advance...
May

Hi All,

Well I did look at the links you gave and the phpmyadmin isn't what I was looking for, and as for the website link, I actually looked at that site before, and it helps me understand a lil bit about the uploading but it does not allow the user to actually select a file (with a browse button...sorta like thos picture uploader, but this time it's a CSV uploader)...I hope I am not confusing you...

How about letting MySQL do the dirty work:

load data local infile 'infile.csv' into table yourtable
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(column1,column2,column3)

This example is for a line that looks like this: 'data','data','data' (delimited by a comma and enclosed by quotes

Be sure to remove any headers or other non-important data from the csv file

Yea, but the files to be uploaded are monthly...and are therefore named differently...so I can't have a static name in the code...

niek_e provided a good example. Use what he has provided, and then implement an upload script. Just substitute the 'infile.csv' for the file that has been uploaded. You're asking for a lot, eh? Everything done for you? Teach a man to fish... :)

Hi,
Ok, I'll give it a try and keep you all posted...:)
Oh, and nope, I do not want that everything be done for me...I was merely stating what I want to get done, and if there's anyone who can guide me along....:)...if it came across that way, then I must say that that was not my intention...
so, like I said, I'll give niek_e's example a shot, and I'll let you all know...:)
Thanks much in advance...:)
May

No no, you're fine... maybe start a new thread about handling PHP file uploads.

So that piece of code works, I am able to upload the data into a table in the database....but that was done through MySQL Query Browser...I gotta get that piece of code to work with my upload button now...

Ok, well I am closing this thread...I will start with a new thread...

<?php
	$dbhost = 'localhost';
	$dbuser = 'root';
	$dbpass = '';
	$dbname = 'dbname';
	$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
	mysql_select_db($dbname);
	
         $fname ="something.csv";
        $chk_ext = explode(".",$fname);
        if(strtolower($chk_ext[1]) == "csv")
         {
        
             $filename = "something.csv";
             $handle = fopen($filename, "r");
       
             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
             {
				 //print_r($data);
                $sql = "INSERT into users(name,lastname,email) values('$data[0]','$data[1]','$data[2]')";
                
				
				mysql_query($sql) or die(mysql_error());
             }
       
             fclose($handle);
             echo "Successfully Imported";
         }
         else
         {
             echo "Invalid File";
         }   
   ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.