PHP MySQL LOAD DATA INFILE Help

I just can't get this request correctly. Basically, I take csv from the form and try to load it into the database. I took most of the requests from phpmyadmin. Should I use a temporary file correctly? here it is...

<form name = "price_chart" method="post" action="index.php?option=<?php echo $option ?>&task=complete" enctype="multipart/form-data">
<label>File: </label>
<input type="file" name="white" id="white"/><br />
<input type="hidden" value="TEST" name = "test" />
<input type="submit" name = "upload" value="Upload File" />
</form>

<?php
}
function complete($option){
$pfile = $_FILES['white']['name'];  
$ptmpName = $_FILES['white']['tmp_name'];
$test = $_POST['test'];
$result = mysql_query("LOAD DATA LOCAL INFILE '$ptmpName' REPLACE INTO TABLE 'jos_rates_table' FIELDS TERMINATED BY ',' ENCLOSED BY ' ' ESCAPED BY '\\' LINES TERMINATED BY '\n'('country' , 'rate')")or die ('Error: '.mysql_error ());
        while ($row = mysql_fetch_array($result)) {
        }
        $num_rows = mysql_num_rows($result);
        echo $num_rows;
}

UPDATE: here is the error message

You have an error in the SQL syntax; check the manual that matches your MySQL server version for the correct syntax to use next to '' jos_rates_table 'FIELDS TERMINATED BY', 'ENCLOSED BY' 'ESCAPED BY' \ 'LINES' on line 1

+3
source share
2 answers

You need to avoid the slash for the line termination clause, otherwise you just added a new line to your query.

LINES TERMINATED BY '\\n'

Mysql \n, PHP , .

, , .

+3

, URL- index.php ''? , ENCTYPE = "multipart/form-data", . , , , ,

$pfile = $_FILES['white']['name']; 
$ptmpName  = $_FILES['white']['tmp_name']; 
$fileSize = $_FILES['white']['size']; 
$fileType = $_FILES['white']['type']; 
$fp = fopen($ptmpName, 'r'); 
$content = fread($fp, $fileSize); 
$content = str_replace("\r\n", "\r", $content);
$Data_Lines = explode("\r",$content);

for ($n = 0; $n < (count($Data_Lines)-1);$n++){
$line =  addslashes($Data_Lines[$n]); 
$Value = explode(",",$line);
...//The you run insert  statements for $Value[1], $Value[2]...///

}

fgetcsv - : http://www.programmingfacts.com/import-csvexcel-data-mysql-database/

-1

All Articles