Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用IF语句用CSV中的值填充列_Php_Mysql_Excel_Csv - Fatal编程技术网

Php 如何使用IF语句用CSV中的值填充列

Php 如何使用IF语句用CSV中的值填充列,php,mysql,excel,csv,Php,Mysql,Excel,Csv,我的脚本需要帮助。我正在上传一个CSV文件,它有两列matric_no和score。问题是,我试图在maintable的grades列中填充条目,这取决于每行分数列中上载的内容。即,如果分数>70,则分数中的is值应为a,依此类推。我主桌的结构是 MAINTABLE score_id matric_no score grade course_code level semester_name session 最后四列由用户从界面输入的值填充,will grade是我想要动态生成的 谢谢 <

我的脚本需要帮助。我正在上传一个CSV文件,它有两列matric_no和score。问题是,我试图在maintable的grades列中填充条目,这取决于每行分数列中上载的内容。即,如果分数>70,则分数中的is值应为a,依此类推。我主桌的结构是

MAINTABLE
score_id
matric_no
score
grade
course_code
level
semester_name
session
最后四列由用户从界面输入的值填充,will grade是我想要动态生成的

谢谢

<?php   

$fname = $_FILES['csv_file']['name'];     
$chk_ext = explode(".",$fname);             

        $filename = $_FILES['csv_file']['tmp_     name'];   
$handle = fopen($filename, "r");      
if(!$handle){
die ('Cannot open file for reading');
}      
              while (($data = fgetcsv($handle,     10000, ",")) !== FALSE)
{
           $query = "INSERT INTO maintable         (matric_no, score, session, semester_      name, course_code, level)   
                values('$data[0]', '$data[1]', '".           $session."', '".$semester_name."', '".        $course_code."', '".$level."') 
 ON DUPLICATE KEY UPDATE matric_no =     matric_no"; 
mysql_query($query) or die(mysql_error     ());
} 
 if ($row['score'] >= 70) {
              $grade = 'A';  
             }
             elseif ($row['score'] >= 60) {
            $grade = 'B';
            }elseif ($row['score'] >= 50) {
            $grade = 'C';
            }elseif ($row['score'] >= 45) {
            $grade = 'D';
            }elseif($row['score'] >= 40) {
            $grade = 'E';
            }else{
            $grade = 'F';  
            }  

$query = "UPDATE maintable 
SET grade = 'A' WHERE score >= 70, 
SET grade = 'B' WHERE score >= 60  AND     score < 70 ,
SET grade = 'C' WHERE score >= 50  AND     score < 60,
SET grade = 'D' WHERE score >= 45  AND     score < 50,
SET grade = 'E' WHERE score >= 40  AND     score < 45 ,
SET grade = 'F' WHERE score < 40"               mysql_query($query) or die(mysql_error       ());
 fclose($handle);
  ?>
使用

UPDATE maintable
SET grade = CASE
    WHEN score >= 70 THEN 'A'
    WHEN score >= 60 THEN 'B'
    WHEN score >= 50 THEN 'C'
    WHEN score >= 40 THEN 'D'
    WHEN score >= 30 THEN 'E'
    ELSE 'F'