未定义的偏移量2

我试图在php中导入.csv文件,但我得到一个错误undefined offset:2和插入我的数据库中的数据只有0.有人可以帮我或给我线索如何解决这个问题?

这是错误。 在这里输入图像说明

更新当我尝试var_dump的结果是.. 在这里输入图像说明

这是我的代码

 <?php include 'db.php'; if(isset($_POST["Import"])){ echo $filename=$_FILES["file"]["tmp_name"]; if($_FILES["file"]["size"] > 0) { $file = fopen($filename, "r"); while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) { //It wiil insert a row to our subject table from our csv file` $sql = "INSERT into accounts (`acct_code`, `acct_name`) values('$emapData[1]','$emapData[2]')"; //we are using mysql_query function. it returns a resource on true else False on error $result = mysql_query( $sql, $conn ); if(! $result ) { echo "<script type=\"text/javascript\"> alert(\"Invalid File:Please Upload CSV File.\"); window.location = \"index.php\" </script>"; } } fclose($file); //throws a message if data successfully imported to mysql database from excel file echo "<script type=\"text/javascript\"> alert(\"CSV File has been successfully Imported.\"); window.location = \"index.php\" </script>"; //close of connection mysql_close($conn); } } ?> 

值$ emapData [2]未设置。 这意味着您的CSV数据没有该行第三列的数据。 请检查您的csv文件。如果第一列和第二列是您需要从csv插入到数据库中,那么您的查询值应该是$ emapData [0],$ emapData [1]

 $sql = "INSERT into accounts (acct_code, acct_name) values($emapData[0],'$emapData[1]')";