dongnaigu2052 2015-12-08 13:55
浏览 38

PHP / MYSQL - 将Excel / CSV上传到MySql表

I recently built a web app which allows teachers to give grades to students and a lot of other stuff. Right now I'm adding a feature that gives the teacher the option to upload an excel/csv file with all the grades to the different students. My code seems fine however there are some issues with it and I can't figure out why. What happens is I get a bunch of blank records on my 'avaliacoes' table when I should only be getting 2 records.

How my excel looks :

1 (row) :  naluno, uc, tipo, nota
2 (row) : r2011251, BD, exame, 15
3 (row) : r2011223, BD, exame, 16

(the first row isn't accounted for when inserting into the table)

My HTML code for the form (inside Diretor-Curso.php) :

<form class="form-group" action="lancarnota3.php" method="post" enctype="multipart/form-data">
  <label class="control-label" for="uc">Enviar Ficheiro Excel/CSV com notas</label>

  <br>
  <input id="fileSelect" type="file" name="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />  
  <br>

  <input type="submit" class="btn btn-default botao2" name="Submeter" value="Submeter"></button>

</form>

My PHP page (lancarnota3.php) which handles the upload :

<?php

if(isset($_POST["Submeter"]))
{
    $conn = mysqli_connect("localhost", "root", "", "teste");
    $conn->set_charset("utf8");

    // Check connection
    if($conn === false){
        die("ERROR: Could not connect. " . mysqli_connect_error());
    }
    //$filename=$_FILES["file"]["tmp_name"];
    if($_FILES["file"]["size"] > 0)
    {
        $file = fopen($_FILES["file"]["tmp_name"], "r");

        $count = 0;                                         
        while (($emapData = fgetcsv($file, 1000, ",")) !== FALSE)
        {

            $count++;                                      

            if($count>1){                                  
              $sql = "INSERT into avaliacoes (naluno, uc, tipo, nota) values ('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]')";
              mysqli_query($conn, $sql);
            }                                              
        }
        fclose($file);
        include 'Diretor-Curso.php';
        echo "<script> replace('lancarnotas2'); </script>";
    }
    else
    {
        include 'Diretor-Curso.php';
        echo "<script> replace('lancarnotas6'); </script>";
    }
}


// close connection
mysqli_close($conn);
?>

Any idea how to make it work?

  • 写回答

2条回答 默认 最新

  • 普通网友 2015-12-08 16:03
    关注

    Are you sure that your data file doesn't contain empty line? Check the MySQL general log to see which queries were executed. I expect you will see the queries inserting the blank records in the log.

    You could simply add a condition before the INSERT to check that you are trying to insert "non-empty" data, or perform additional validation to ensure that the data is indeed complete.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值