我不能用php从excel导入mysql

jm81lzqq  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(325)

有了这个更新,它不会显示任何错误,当我发送打印时,它会显示我想添加到数据库中的信息,但它不会添加它。。。

if(isset($_POST["submit_file"]))
$file = $_FILES["file"]["tmp_name"];
$file_open = fopen($file,"r");
$count = 0; 
while(($csv = fgetcsv($file_open, 100, ",")) !== false) 
{ 
      $agente = isset($csv[0])? $csv[0]: null;
    $revenue = isset($csv[1])? $csv[1]: null;
    $css = isset($csv[2])? $csv[2]: null;
    $rating = isset($csv[3])? $csv[3]: null;
    $absent = isset($csv[4])? $csv[4]: null;
    $score = isset($csv[5])? $csv[5]: null;
    $count++;
    if($count>1){ 
      $query = "INSERT INTO metricas values ('$agente', $revenue, $css, $rating, $absent, $score)";
  print $query;  }

     }

?>

aor9mmx1

aor9mmx11#

学会正确地缩进代码,这将有助于找出引号、括号、方括号和大括号的问题。
您也可以使用[php代码检查器][https://phpcodechecker.com/]查找代码中的解析错误。

<?php
require_once 'connect.php';

if(isset($_POST["submit_file"]))
$file = $_FILES["file"]["tmp_name"];
$file_open = fopen($file,"r");
$count = 0; 
while(($csv = fgetcsv($file_open, 100000, ",")) !== false) {
    //$agente[$csv[0]] = isset($csv[1])? $csv[1]: null; // This is wrong in terms of semantics. It should be as per the following line
    $agente = isset($csv[1])? $csv[1]: null;
    // Below lines also should be changed. Hence did change them.
    $revenue = isset($csv[2])? $csv[2]: null;
    $css = isset($csv[3])? $csv[3]: null;
    $rating = isset($csv[4])? $csv[4]: null;
    $absent = isset($csv[5])? $csv[5]: null;
    $score = isset($csv[6])? $csv[6]: null;
    $count++;
    if($count>1){ 
        $query = "INSERT INTO metricas values (null, '$agente', $revenue, $css, $rating, $absent, $score)";
    }
}   //  Added this to close the while loop.

?>

编辑修改代码取出 Warning: Array to String conversion .

相关问题