KN1V3S Posted September 7, 2021 Share Posted September 7, 2021 (edited) 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 September 7, 2021 by KN1V3S Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/ Share on other sites More sharing options...
Barand Posted September 7, 2021 Share Posted September 7, 2021 Do you have empty lines in the CSV file? Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589716 Share on other sites More sharing options...
ginerjm Posted September 7, 2021 Share Posted September 7, 2021 Perhaps you might try doing the upload and seeing what the contents of the file are. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589720 Share on other sites More sharing options...
KN1V3S Posted September 7, 2021 Author Share Posted September 7, 2021 6 hours ago, Barand said: Do you have empty lines in the CSV file? This is what my CSV file looks like in Excel Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589726 Share on other sites More sharing options...
KN1V3S Posted September 7, 2021 Author Share Posted September 7, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589727 Share on other sites More sharing options...
KN1V3S Posted September 7, 2021 Author Share Posted September 7, 2021 At the moment I'm not getting the 'Undefined offset' error anymore, but now it seems like it just wouldn't upload the arrays of the csv into my database. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589728 Share on other sites More sharing options...
ginerjm Posted September 7, 2021 Share Posted September 7, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589730 Share on other sites More sharing options...
Barand Posted September 7, 2021 Share Posted September 7, 2021 Your query is trying to insert $column[0], $column[1], $column[3], $column[4], $column[5], According to your var_dump you have $column[0], $column[1], $column[2], $column[3] Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589732 Share on other sites More sharing options...
dodgeitorelse3 Posted September 7, 2021 Share Posted September 7, 2021 I see in your array that you printed out that there is an encoding issue (the diamonds with the question mark) Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589733 Share on other sites More sharing options...
maxxd Posted September 8, 2021 Share Posted September 8, 2021 Excel sometimes has issues with character encoding in it's CSV export - I ran into this just the other week. You'll need to scrub your input before you can actually use it. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589736 Share on other sites More sharing options...
KN1V3S Posted September 8, 2021 Author Share Posted September 8, 2021 Thank you all for your replies, I‘ll take a look into them later when I‘m home! I really appreciate your help guys :) Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589740 Share on other sites More sharing options...
KN1V3S Posted September 9, 2021 Author Share Posted September 9, 2021 So I reviewed everything, I adjusted my query part but it still wouldnt work. The only thing I might be thinking is that my CSV formatting is wrong but I'm not sure. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589749 Share on other sites More sharing options...
KN1V3S Posted September 9, 2021 Author Share Posted September 9, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589750 Share on other sites More sharing options...
Barand Posted September 9, 2021 Share Posted September 9, 2021 As I pointed out 6 replies ago, your data contains 4 columns, your query is trying to process 5 columns. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589755 Share on other sites More sharing options...
KN1V3S Posted September 9, 2021 Author Share Posted September 9, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589757 Share on other sites More sharing options...
Barand Posted September 9, 2021 Share Posted September 9, 2021 What's the new code that doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589759 Share on other sites More sharing options...
KN1V3S Posted September 9, 2021 Author Share Posted September 9, 2021 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> Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589760 Share on other sites More sharing options...
Solution ginerjm Posted September 9, 2021 Solution Share Posted September 9, 2021 Your query is trying to insert 5 values into 4 columns. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589761 Share on other sites More sharing options...
KN1V3S Posted September 9, 2021 Author Share Posted September 9, 2021 6 minutes ago, ginerjm said: Your query is trying to insert 5 values into 4 columns. Oh god thanks xD Now I feel really stupid for not noticing that. Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589763 Share on other sites More sharing options...
KN1V3S Posted September 9, 2021 Author Share Posted September 9, 2021 29 minutes ago, Barand said: What's the new code that doesn't work? I believe you meant that with your reply, sorry for not understanding it. I've fixed it now! Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589764 Share on other sites More sharing options...
KN1V3S Posted September 9, 2021 Author Share Posted September 9, 2021 Thank you all for your help guys, I really appreciate you taking your time for my problem. Have a nice day! Quote Link to comment https://forums.phpfreaks.com/topic/313681-notice-undefined-offset-csv-fileupload-php/#findComment-1589765 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.