Uploading values from Excel into MySQL
Posted: Wed Jan 26, 2011 2:39 pm
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?
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
?>