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

Yong Scott Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.