0
//connect to Database

mysql_select_db($database_csv, $csv);

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

if (is_uploaded_file($_FILES['filename']['tmp_name'])) {

echo "<br><center><p>" . " ". $_FILES['filename']['name'] ." " . "</p></center>";
                                                        }

//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$firstRow = true;
$count = 0; //skip first line of the CSV file 
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {

if($count) //skip first line of the CSV file 
{

$name=$data[0];

$description=$data[1];

$price=$data[2];

$shipping=$data[3];

$quantity=$data[4];

$import="INSERT into results (name,description,price,shipping,quantity) VALUES('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";

mysql_query($import) or die(mysql_error());
}
$count++;
}

fclose($handle);

?>

Uploading .CSV File using FileUpload in PHP/Mysql but the data in the csv all insert into one column amongs five columns, please read my code and help. Thanks in Advance..

2 Answers 2

2

A simple LOAD DATA INFILE will work just fine. It will also be a lot faster.

Code:

<?php
//connect to Database
mysql_select_db($database_csv, $csv);

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

    $tmp = $_FILES['filename']['tmp_name'];
    $name = $_FILES['filename']['name'];

    //Can be any full path, just don't end with a /. That will be added in in the path variable
    $uploads_dir = 'C:/Users/Ernest/Desktop';

    $path = $uploads_dir.'/'.$name;

    if(move_uploaded_file($tmp, $path)){
        echo "<br><center><p>". $name ."</p></center>";

        //Import uploaded file to Database
        //If the query fails, try LOAD DATA LOCAL INFILE
        $import = "
        LOAD DATA INFILE '".$path."'
               INTO TABLE results  CHARACTER SET utf8 FIELDS TERMINATED BY ','
               OPTIONALLY ENCLOSED BY '\"' IGNORE 1 LINES (name, description, price, shipping, quantity);
        ";

        mysql_query($import) or die(mysql_error());
        //If you do not want to keep the csv, you can delete it after this point.
        //unlink($path);

    }else{
        echo 'Failed to move uploaded files';
    }

}
?>
Sign up to request clarification or add additional context in comments.

8 Comments

i tried it but still got the following error:File 'C:xampp mpphp6A79.tmp' not found (Errcode: 22)
Try LOAD DATA LOCAL INFILE
It appears to be the same error: File 'C:xampp mpphpE5D0.tmp' not found (Errcode: 22). But when i make my file location known it works like C:/Users/Ernest/Desktop/Book1.csv in the LOAD DATA INFILE. Thanks please help.
The tmp_name is the problem now... 'C:xampp mpphpE5D0.tmp' not found
I updated my answer with moving the uploaded file to your desktop and then LOAD DATA INFILE with that path. You can change that path to whatever you would like. Also, make sure the file is moved to a different folder than the source file folder.
|
0

Try this:

if($count) //skip first line of the CSV file 
{

$data[0]=$data[0];

$data[1]=$data[1];

$data[2]=$data[2];

$data[3]=$data[3];

$data[4]=$data[4];

$import="INSERT into results (name,description,price,shipping,quantity) VALUES('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";

mysql_query($import) or die(mysql_error());
}
$count++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.