user1183307
user1183307

Reputation:

CSV into MySQL using PHP

I'm trying to import data from my students.csv file into mysql using php. The entries in the csv file is in such a way that column (student_number, fname, lname, level) will be inserted into biodata table..

I'm also uploading the student.csv file from my computer.

When I run the page I dont get anything out on the screen.

session_start();
require('includes/dbconnect.php');
require 'includes/header.inc.php';

//check for file upload
if (isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])) {
    //upload directory
    $upload_dir = "C:\Users\DOTMAN\Documents\students.csv";
    //create file name
    $file_path = $upload_dir . $_FILES['csv_file']['name'];
    //move uploaded file to upload dir
    if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {
        //error moving upload file
        echo "Error moving file upload";
    }
    //open the csv file for reading
    $handle = fopen($file_path, 'r');
    //turn off autocommit and deletethe bio data
    mysql_query("SET AUTOCOMMIT=0");
    mysql_query("BEGIN");
    mysql_query("TRUNCATE TABLE biodata") or die(mysql_error());
    while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
        //Access field data in $data array ex.
        $student_number = $data[0];
        $fname = $data[1];
        $lname = $data[2];
        $level = $data[3];
        //Use data to insert into db
        $query = "INSERT INTO biodata (student_number, fname, lname, level)
                  VALUES ('$student_number', '$fname', '$lname', '$level')";
        mysql_query($query) or die (mysql_error());
    }
}

Upvotes: 0

Views: 482

Answers (5)

Starx
Starx

Reputation: 78961

Solution using PHP

$file = 'path/to.csv'; 

$lines = file($file);
$firstLine = $lines[0];
foreach ($lines as $line_num => $line) {
    if($line_num==0) { continue; } //escape the header column
    $arr = explode(",",$line);
    $column1= $arr[0];
    $column2= $arr[1];
       
    echo $column1.$column2."<br />";
        //put the mysql insert statement here
}

Upvotes: 0

PHP Connect
PHP Connect

Reputation: 549

Have u debug the $_FILES:

print_r($_FILES); 

before doing any thing

Upvotes: 0

Aleks G
Aleks G

Reputation: 57306

One immediate issue I can see is here:

$upload_dir = "C:\Users\DOTMAN\Documents\students.csv";

//create file name
$file_path = $upload_dir . $_FILES['csv_file']['name'];

You are already assigning the entire path, including the file name, to the $upload_dir variable - and then you're appending the uploaded file name again.

If you think there are errors in your code, start by adding

ini_set('display_errors', 1);
error_reporting(E_ALL);

to the beginning of your PHP code and fix any warnings/errors displayed. You can then turn off printing error messages by changing the second parameter to 0 in the first call.

Upvotes: 0

Ethan
Ethan

Reputation: 2784

if you only need to do this once, i would consider using something like: http://csv2sql.com/

Upvotes: 0

Devart
Devart

Reputation: 121902

I'd suggest you to upload CSV-file with LOAD DATA INFILE command. This is fast method.

Upvotes: 1

Related Questions