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

include 'db.php';

		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`) 
	         //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\"

	         //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\"

			 //close of connection

Open in new window

Scott Yong Asked:
Who is Participating?
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() due to the deprecation of the mysql_ functions. You should be using mysqli_ functions instead, like you did in the first script you had.
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.
Julian HansenCommented:
PHP arrays are 0 based - so the first element would be

Open in new window

And the 6th would be

Open in new window

Further to Olaf's comment ( the MySQL library has been deprecated and removed in later versions of PHP.

Consider using MySQLI as it is a close substitute for mysql.
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.