-1

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. enter image description here

here's the result. The output should be 75,80,64 not 64,64,64 enter image description here

here's my database structure enter image description here

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); 



         }

    }   
nethken
  • 1,072
  • 7
  • 24
  • 40

1 Answers1

0

Your loop for updating in mysql is done just after your fetch your last value In means in your code you do 3 loops for updating all value

$sql1 ="SELECT * FROM grade WHERE subj_descr ='$sub' AND section = '$section'";

This loop which fetch always the same result is called 3 times.

"UPDATE grade SET midterm_grade = '$last' WHERE grade_id = '$grade_id'";

In this query the grade_id comes from all the resultset

Basically you are doing this 1- get the last value of the line of CSV 2- select all records 3- update all of them with the value of 1- 4- next line

Instead of looping in all you mysql database for the SELECT, you should be able to SELECT just the record you need. I don't know if your CSV is full but do you have the grade_id in a column ? Otherwise how can you match a row of your CSV to a record in your database ?

EDIT

after discussion you said you have a unique key on section, term and subject (You have to identify a relation between a row in CSV and a row in your database)

$last = end($emapData); //to get the last column
$section = $emapData[0]; // assuming section is the first column of CSV
$term = $emapData[1]; // assuming term is the 2nd column of CSV
$subject = $emapData[2]; // assuming subject is the 3rd column of CSV

$sql = "UPDATE grade SET midterm_grade = '$last' WHERE section = '$section' AND term = '$term' AND subject = '$subject'";
$result = mysqli_query( $con, $sql );
Sylwit
  • 1,497
  • 1
  • 11
  • 20
  • Every row have an grade_id sir. I don't know how to use the grade_id for that. – nethken Aug 30 '16 at 02:57
  • All of the record are needed sir. Every .csv contains a section of student. I don't know how to update it with just one go. – nethken Aug 30 '16 at 02:59
  • You confirm that you do have a grade_id in the csv ? – Sylwit Aug 30 '16 at 03:00
  • Yes. I post the structure of my database to my question. – nethken Aug 30 '16 at 03:01
  • I don't see the grade_id in csv that's why I ask – Sylwit Aug 30 '16 at 03:02
  • But how can i use the grade_id if it's just only a file_upload? – nethken Aug 30 '16 at 03:04
  • I edited my answer to show you how getting the grade_id from CSV then use it directly in your UPDATE query. But I asked you about grade_id in CSV and you always answer about your database which is useless. – Sylwit Aug 30 '16 at 03:10
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122152/discussion-between-sylwit-and-nethken). – Sylwit Aug 30 '16 at 03:14