I'm trying to update a specific column through fgetcsv
. But the problem is all the data are the same. Can someone help me about this? I don't know how to make the use of grade_id
here because there are no grade_id
in csv only in the database. And im doing it with only just file uploading.
Here's the csv. I just only want the midterm grade to be updated. But only the value 64
is inserted.
here's the result. The output should be 75,80,64
not 64,64,64
here's my code
if(isset($_POST["Import"])){
$term = $_POST['term'];
$fac_code = $_POST['fac_code'];
$sch_year = $_POST['schoolyear'];
$section = $_POST['sec'];
$semester = $_POST['semester'];
$sub = $_POST['sub'];
echo $filename=$_FILES["file"]["tmp_name"];
$heading = true;
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
if($heading) {
// unset the heading flag
$heading = false;
// skip the loop
continue;
}
//to get the last column
$last = end($emapData);
$sql1 ="SELECT * FROM grade WHERE subj_descr ='$sub' AND section = '$section'";
$result = mysqli_query($con, $sql1);
while($row = mysqli_fetch_array($result)){
$gradeid = $row['grade_id'];
$sql = "UPDATE grade SET midterm_grade = '$last' WHERE grade_id = '$grade_id'";
$result = mysqli_query( $con, $sql );
}
}
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 = \"homefaculty.php\"
</script>";
//close of connection
mysqli_close($con);
}
}