Error on php

Notice: Undefined offset: 6 in C:\xampp\htdocs\display\import.php on line 18

Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\display\import.php:20 Stack trace: #0 {main} thrown in C:\xampp\htdocs\display\import.php on line 20


<?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 subject (`(`name`, `cpu_count`, `memory_size`, `disk_space_size`,`nic_count`, `power_state`) 
	            	values('$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]')";
	         //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); 
				
		 	
			
		 }
	}	 
?>		 

Open in new window

Scott Yong Asked:
Who is Participating?
 
gr8gonzoConsultantCommented:
Just copying my comment from last night:

Okay, so first the undefined offset problem is simply the result of you starting your array indexes at 1. In PHP, arrays start at zero, so the first element in your array is $emapData[0], not $emapData[1]. So the error is saying that there is no index "6", which means  $emapData[6] doesn't exist. To solve this, just shift all your index numbers down by one so your last element is $emapData[5], not $emapData[6].

The second problem:
Call to undefined function mysql_query()

...is due to the deprecation of the mysql_ functions. You should be using mysqli_ functions instead, like you did in the first script you had.
0
 
Olaf DoschkeSoftware DeveloperCommented:
PHP 7?

mysql functions deprecated since many years in PGP 5.x and are removed from PHP7. You need to port to mysqli or PDO.

Bye, Olaf.
0
 
Julian HansenCommented:
PHP arrays are 0 based - so the first element would be
emapData[0]

Open in new window

And the 6th would be
emapData[5]

Open in new window


Further to Olaf's comment (http://php.net/manual/en/migration55.deprecated.php) the MySQL library has been deprecated and removed in later versions of PHP.

Consider using MySQLI as it is a close substitute for mysql.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.