Uploading values from Excel into MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nugget007
Forum Newbie
Posts: 1
Joined: Wed Jan 26, 2011 2:28 pm

Uploading values from Excel into MySQL

Post by nugget007 »

Hello, I have created a web page where users are able to upload an Excel spread sheet onto the server, which populates my database with the values. But I am unable to upload the string values. The values which update my database is the studentId, tutorId, part, email, firstname and surname. No errors are showing, only the module Code shows 0s and updating the rest of the database correctly.

Here is my code where I think something is going wrong. Could anyone shed some light on this please?

Code: Select all

		<?php
		//truncate table
		mysqli_fetch_array(mysqli_query($con,'TRUNCATE TABLE `student`'));
		mysqli_fetch_array(mysqli_query($con,'TRUNCATE TABLE `stumod`'));
		//code to import into db from $importData
		if(is_array($importData)){
			foreach($importData as $d){
				//insert student row
				mysqli_fetch_array(mysqli_query($con,'INSERT INTO `student` (`studentId`,`tutorId`,`part`,`email`,`firstname`,`surname`) VALUES ('.(int)$d[1].','.(int)$d[7].','.(int)$d[8].',\''.($d[9]).'\',\''.$d[3].'\',\''.$d[2].'\')'));
				//insert 3 modules - from the 3 columns on the spreadsheet
				mysqli_fetch_array(mysqli_query($con,'INSERT INTO `stumod` (`studentId`,`moduleCode`) VALUES ('.(int)$d[1].',\''.mysql_real_escape_string($d[4]).'\')'));
				mysqli_fetch_array(mysqli_query($con,'INSERT INTO `stumod` (`studentId`,`moduleCode`) VALUES ('.(int)$d[1].',\''.mysql_real_escape_string($d[5]).'\')'));
				mysqli_fetch_array(mysqli_query($con,'INSERT INTO `stumod` (`studentId`,`moduleCode`) VALUES ('.(int)$d[1].',\''.mysql_real_escape_string($d[6]).'\')'));
			}
			//voila
		}
    } else {
		//PRINT AN ERROR IF THE FILE COULD NOT BE COPIED
		?>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Uploading values from Excel into MySQL

Post by Jade »

Are you sure the array with data pulled from the excel sheet isn't causing the problem? That's where I would start.
Post Reply