Jump to content

Uploading direct in to mySQL - Help


spires

Recommended Posts

Hi

 

I'm trying to upload a csv direct into my data base using the 'BULK INSERT'.

However, the code below is throwing up this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BULK INSERT test_upload FROM 'upload.csv' WITH ( FIR' at line 1

 

Can any one see the error.

Thanks

 

<?PHP
include("dbconnect.php");


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

     $userfile_name = $_FILES['input']['name']; 


$sql = "BULK INSERT test_upload 
	FROM '$userfile_name' 
	WITH 
	( 
			FIRSTROW = 2, 
			MAXERRORS = 0, 
			FIELDTERMINATOR = ',', 
			ROWTERMINATOR = '\n' 
	)";

$query = mysql_query($sql) or die (mysql_error());


if ($query){
echo '1';
}else{
echo '2';
}

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form action="test_upload.php" method="post" name="form1" enctype="multipart/form-data">
<input type="file" name="input" />
<br>
<input type="submit" value="submit" name="submit" />
</form>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/153624-uploading-direct-in-to-mysql-help/
Share on other sites

I'm not familiar with BULK INSERT (i didn't even know that exist) but i can guess mysql will need the full pathname/filename instead of only the filename.

 

Try echo $sql; and run the sql command from phpmyadmin or in a shell to see if it work.

 

You may want to take a look at this :

http://dev.mysql.com/doc/refman/5.1/en/load-data.html

 

If it fail i'm sure fgetcsv with a loop and lot of insert will work, i'm not sure about the performance.

 

http://www.php.net/manual/en/function.fgetcsv.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.