Link to home
Start Free TrialLog in
Avatar of Geerd
Geerd

asked on

Import CSV file into MySQL database using PHP

Dear reader,

I'm trying to import 2 CSV files into temptables in my MySQL database.
In my local development environment it works fine, but when I upload the file to the server I get an error saying the query cannot be executed. It concerns the query below.

$import = "INSERT INTO temp_grau1 (CODI,CONCEPTA,TAMANY,PVP,PREU_ESPECIAL,ANYADA,STOCK) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]')";

I've also attached the code snipped.

Any help is very much appriciated!

Kind regards.

Geerd


if(isset($_POST['submit'])){

			//leegmaken tabellen

			$result = mysql_query("DELETE FROM temp_grau1",$db);
			$result = mysql_query("DELETE FROM temp_grau2",$db);

     			$filename=$_POST['filename1'];

   			$handle = fopen("$filename", "r");

    			while (($data = fgetcsv($handle, 10000, ";")) !== FALSE){

			        $import = "INSERT INTO temp_grau1 (CODI,CONCEPTA,TAMANY,PVP,PREU_ESPECIAL,ANYADA,STOCK) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]')";

  				mysql_query($import) or die ("Kan de query1 niet uitvoeren.");

    			}

     			fclose($handle);

     			print "Import LIJST 1 done";

     			$filename=$_POST['filename2'];

   			$handle = fopen("$filename", "r");

    			while (($data = fgetcsv($handle, 10000, ";")) !== FALSE){

			        $import = "INSERT INTO temp_grau2 (CODI,CONCEPTA,TAMANY,PVP,PREU_ESPECIAL,ANYADA,STOCK) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]')";

  				mysql_query($import) or die ("Kan de query2 niet uitvoeren.");

    			}

     			fclose($handle);

     			print "Import LIJST 1 done";

			?><META HTTP-EQUIV="refresh" CONTENT="0; url=../Menu_Prijslijst.php"><?php



		}else{

			print "<br><br><center>Please enter the URL of the files you want to use for the price comparison.";

			print "<form action='Inladen.php' method='post'>";

      			print "Pricelist file 1 (previous file):<br>";

      			print "<input type='file' name='filename1' size='20'><br>";

      			print "Pricelist file 2 (current file):<br>";

      			print "<input type='file' name='filename2' size='20'><br><br>";

     			print "<input type='submit' name='submit' value='COMPARE LISTS'></form><center>";

   		} 

		?>

Open in new window

Avatar of fabzster187
fabzster187
Flag of South Africa image

U could just use PHPMYADMIN

http://www.phpmyadmin.net/home_page/index.php

there could be many reasons
1) Is the table in the server has the enough rights
2) If the table columns names are in lower case

You need to show whats the error message that you got use the following to print the error: mysql_error()

Hope this helps

Avatar of Geerd
Geerd

ASKER

Dear Shinug,

Thank you for the reply. The error message is:

Access denied for user 'XXXXXX'@'localhost' (using password: NO)

I've replaced my name with XXXXXXX in this error message.

Kind regards,

Geerd
ASKER CERTIFIED SOLUTION
Avatar of Shinesh Premrajan
Shinesh Premrajan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Geerd

ASKER

You are right. I don't know why, but I uploaded an old DB connect file ;-)

Oeps.

Thank you !!!