I'm working on a website that required me to create function to upload .csv file. When upload the file, it says wrong format, and it display this message:
C:\xampp\tmp\php9F4F.tmp
p/s:I already convert the excel file into .csv comma delimited format and still get error while uploading.
What should I do to resolve the error and then get to upload the csv file into my database?
stdreport.php : //im using the same code from import.php in here to connect it with database
<?php
$SQLSELECT = "SELECT stdCard, stdName, stdProgram, stdCourseDesc, stdCampus
FROM student
ORDER BY stdID
LIMIT 0,20";
$result_set = mysql_query($SQLSELECT, $conn);
while($row = mysql_fetch_array($result_set))
{
?>
import.php
<?php
$conn=mysql_connect("localhost","root","") or die("Could not connect");
mysql_select_db("dashboard",$conn) or die("could not connect database");
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 student
(`stdID`, `stdCard`, `stdName`,
`stdOfficialEmail`,`stdEmail`,
`stdContNum`,`stdCourseDesc`, `stdCampus`,
`accessDate`)
values('$emapData[1]','$emapData[2]','$emapData[3]',
'$emapData[4]','$emapData[5]',
'$emapData[6]', '$emapData[7]','$emapData[8]',
'$emapData[9]')";
//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 = \"stdreport.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 = \"stdreport.php\"
</script>";
//close of connection
mysql_close($conn);
}
}
?>