Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Eric Ewers
Eric Ewers
13,976 Points

Importing CSV file into MySQL using PHP

I am successfully able to import a CSV file into my MySQL database, but it adds a blank line in the database at the end.

My guess is that it's this line that is causing the problem, but I need help on fixing it:

while(!feof($handle)) {

Here is my full code:

// Connect to database
require 'assets/inc/config.php';

if ($_FILES[csv][size] > 0) { 
    $handle = fopen($_FILES[csv][tmp_name], "r");
    $headers = fgetcsv($handle, 1000, ",");
    while(!feof($handle)) {
        $data = fgets($handle);
        $listing = explode(",", $data); 
        $Status = 'Confirmed';
        $Conference = isset($_COOKIE['Conference']) ? $_COOKIE['Conference'] : $_COOKIE['Conference'];
        $RegType = 'Onsite';
        $BadgeType = 'Sponsor';
        $FirstName = $listing[0];
        $LastName = $listing[1];
        $Title = $listing[2];
        $Company = $listing[3];
        $Address = $listing[4];
        $City = $listing[5];
        $State = $listing[6];
        $ZipCode = $listing[7];
        $Email = $listing[8];
        $Phone = $listing[9];
        $Industry = $listing[10];
        $Speaker = $listing[11];
        $sql = "INSERT INTO attendees_test (Status, Conference, RegType, BadgeType, FirstName, LastName, Title, Company, Address, City, State, ZipCode, Email, Phone, Industry, Speaker) VALUES
            (
                '$Status',
                '$Conference',
                '$RegType',
                '$BadgeType',
                '$FirstName',
                '$LastName',
                '$Title',
                '$Company',
                '$Address',
                '$City',
                '$State',
                '$ZipCode',
                '$Email',
                '$Phone',
                '$Industry',
                '$Speaker'
            )";
        mysqli_query($con,$sql);
    }
    //redirect 
    header('Location: test.php?success=1'); die;
}

1 Answer

Eric Ewers
Eric Ewers
13,976 Points

I found a solution. I added the following if statement inside the while loop:

if (!empty($data)) {
}

Full Code looks like:

// Connect to database
require 'assets/inc/config.php';

if ($_FILES[csv][size] > 0) { 
    $handle = fopen($_FILES[csv][tmp_name], "r");
    $headers = fgetcsv($handle, 1000, ",");
    while(!feof($handle)) {
        $data = fgets($handle);
        if (!empty($data)) {
            $listing = explode(",", $data); 
            $Status = 'Confirmed';
            $Conference = isset($_COOKIE['Conference']) ? $_COOKIE['Conference'] : $_COOKIE['Conference'];
            $RegType = 'Onsite';
            $BadgeType = 'Sponsor';
            $FirstName = $listing[0];
            $LastName = $listing[1];
            $Title = $listing[2];
            $Company = $listing[3];
            $Address = $listing[4];
            $City = $listing[5];
            $State = $listing[6];
            $ZipCode = $listing[7];
            $Email = $listing[8];
            $Phone = $listing[9];
            $Industry = $listing[10];
            $Speaker = $listing[11];
            $sql = "INSERT INTO attendees_test (Status, Conference, RegType, BadgeType, FirstName, LastName, Title, Company, Address, City, State, ZipCode, Email, Phone, Industry, Speaker) VALUES
                (
                    '$Status',
                    '$Conference',
                    '$RegType',
                    '$BadgeType',
                    '$FirstName',
                    '$LastName',
                    '$Title',
                    '$Company',
                    '$Address',
                    '$City',
                    '$State',
                    '$ZipCode',
                    '$Email',
                    '$Phone',
                    '$Industry',
                    '$Speaker'
                )";
            mysqli_query($con,$sql);
        }
    }
    //redirect 
    header('Location: test.php?success=1'); die;
}