Jump to content

Notice: Undefined offset: , CSV Fileupload PHP


Go to solution Solved by ginerjm,

Recommended Posts

Hello everyone,

I'm new to PHP and I'm trying to upload a CSV-File (Table in Excel saved as CSV). The problem is that when I try to upload the file, I'm getting the error 'Notice: Undefined offset: 1 in 😄 on line 12'. Accordingly, there is a problem with the offsets 1,3,4 and 5. The error message is displayed twice. Here is my code. I'd really appreciate answers from you guys! :)

Have a nice day!

 

<?php

 

$con = mysqli_connect("localhost","root","","database");

 

if(ISSET($_POST["import"])){

    $filename = $_FILES["file"]["tmp_name"];

 

    if($_FILES["file"]["size"] > 0){

        $file = fopen($filename, "r");

 

        while(($column = fgetcsv($file, 1000, ",")) !== FALSE){

            $sqlInsert = "INSERT INTO `test1` (`id`, `firstname`, `lastname`, `gender`, `adress`) VALUES ('" . $column[0] . "', '" . $column[1] . "', '" . $column[3] . "', '" . $column[4] . "', '" . $column[5] . "' )";

 

            $result = mysqli_query($con, $sqlInsert);

 

            if(!empty($result)){

                echo "CSV Datei wurde erfolgreich in Datenbank eingetragen.";

            }else{

                echo "Es ist ein Fehler aufgetreten.";

            }

        }

    }

}

?>

        <form class ="form" action="testdoc.php" method="post" name="uploadCSV" enctype="multipart/form-data">

            <div>

                <label>CSV Datei auswählen</label>

                <input type="file" name="file" accept=".csv">

                <button type="submit" name="import">Import</button>

            </div>

        </form>

Edited by KN1V3S
6 hours ago, ginerjm said:

Perhaps you might try doing the upload and seeing what the contents of the file are.  

I've added 'var_dump($column);' just to see the array printed out. This is what it looks like with the csv file. I'm clueless, I really don't know how to solve this issue.
 

error php.jpg

A csv file does not produce an array.  It is a string with commas.  Your upload would produce that file on your server.  I was asking what that file really looked like since there seemed to be something wrong with the format.  But - if you are not having an issue any longer my work here is done.

On 9/7/2021 at 9:46 PM, ginerjm said:

A csv file does not produce an array.  It is a string with commas.  Your upload would produce that file on your server.  I was asking what that file really looked like since there seemed to be something wrong with the format.  But - if you are not having an issue any longer my work here is done.

Could you maybe take a look into my csv files? I think they're the issue here.

2 hours ago, Barand said:

As I pointed out 6 replies ago, your data contains 4 columns, your query is trying to process 5 columns.

Hi Barand, yes I've seen your reply. I fixed it, I adjusted the query command to have only 4 columns. It still won't work.

17 minutes ago, Barand said:

What's the new code that doesn't work?

Hey Barand, this is the new code.

 

<?php

$con = mysqli_connect("localhost","root","","csvtest");

if(ISSET($_POST["import"])){

    $filename = $_FILES["file"]["tmp_name"];

    if($_FILES["file"]["size"] > 0){

        $file = fopen($filename, "r");

        while(($column = fgetcsv($file, 10000, ";")) !== FALSE){

            var_dump($column);

            $sqlInsert = "INSERT INTO `test1` (`id`, `firstname`, `lastname`, `gender`, `adress`) VALUES ('$column[0]', '$column[1]', '$column[2]', '$column[3]')";

            $result = mysqli_query($con, $sqlInsert);

            if(!empty($result)){

                echo "CSV Datei wurde erfolgreich in Datenbank eingetragen.";

            }else{

                echo "Es ist ein Fehler aufgetreten.";

            }

        }

    }

}

?>

        <form class ="form" action="testdoc.php" method="post" name="uploadCSV" enctype="multipart/form-data">

            <div>

                <label>CSV Datei auswählen</label>

                <input type="file" name="file" accept=".csv">

                <button type="submit" name="import">Import</button>

            </div>

        </form>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.