代码之家  ›  专栏  ›  技术社区  ›  Carlo Barrogo

使用php导入csv文件,它说它是导入的,但不是

  •  1
  • Carlo Barrogo  · 技术社区  · 6 年前

    我正在测试它,是的,它让我选择一个文件,并sai我“已成功导入”该文件 但当我查看我的数据库时,它并没有导入… …………………

    这是我的函数.php

                 if(isset($_POST["Import"])){
                    $servername = "localhost";
                $username = "root";
                $password = "";
                $db = "lcho_login";
    
                try {
    
                    $conn = mysqli_connect($servername, $username, $password, $db);
                      if($_FILES["file"]["size"] > 0)
                         {
                            $file = fopen($filename, "r");
                            while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
                             {
    
    
                               $sql = "INSERT into lcho_dengue_activities (district_id,barangay_id,month,year,dengue_ind1,dengue_ind2,dengue_ind3,dengue_ind4,dengue_ind5,dengue_ind6,dengue_ind7,dengue_ind8,dengue_ind9,dengue_ind10,dengue_ind11) 
                                   values ('".$getData[0]."','".$getData[1]."','".$getData[2]."','".$getData[3]."','".$getData[4]."','".$getData[5]."','".$getData[6]."','".$getData[7]."','".$getData[8]."','".$getData[9]."','".$getData[10]."')";
                                   echo "yes";
                                   $result = mysqli_query($conn, $sql);
                                if(!isset($result))
                                {
                                    echo "<script type=\"text/javascript\">
                                            alert(\"Invalid File:Please Upload CSV File.\");
                                            window.location = \"imports.php\"
                                          </script>";       
                                }
                                else {
                                      echo "<script type=\"text/javascript\">
                                        alert(\"CSV File has been successfully Imported.\");
                                        window.location = \"imports.php\"
                                    </script>";
                                }
                             }
    
                             fclose($file); 
                         }
                    }
                catch(exception $e)
                    {
                    echo "Connection failed: " . $e->getMessage();
                    }
                    return $conn;   
    
                        $filename=$_FILES["file"]["tmp_name"];      
    
    
    
                    }    
    
    
                 ?>
    

    这是我的imports.php

                <!DOCTYPE html>
                <html lang="en">
    
                <head>
                    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
                    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" crossorigin="anonymous">
                    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script>
    
                </head>
    
                <body>
                    <div id="wrap">
                        <div class="container">
                            <div class="row">
    
                                <form class="form-horizontal" action="functions.php" method="post" name="upload_excel" enctype="multipart/form-data">
                                    <fieldset>
    
                                        <!-- Form Name -->
                                        <legend>Form Name</legend>
    
                                        <!-- File Button -->
                                        <div class="form-group">
                                            <label class="col-md-4 control-label" for="filebutton">Select File</label>
                                            <div class="col-md-4">
                                                <input type="file" name="file" id="file" class="input-large">
                                            </div>
                                        </div>
    
                                        <!-- Button -->
                                        <div class="form-group">
                                            <label class="col-md-4 control-label" for="singlebutton">Import data</label>
                                            <div class="col-md-4">
                                                <button type="submit" id="submit" name="Import" class="btn btn-primary button-loading" data-loading-text="Loading...">Import</button>
                                            </div>
                                        </div>
    
                                    </fieldset>
                                </form>
    
                            </div>
    
                        </div>
                    </div>
                </body>
    
                </html>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   A l w a y s S u n n y    6 年前

    从显示的代码中可以明显看出,您没有在 $sql 尝试在表上插入时查询。因为我看到你 十五 列,但仅通过 十一 价值观。为什么?

    $sql = "INSERT into lcho_dengue_activities (district_id,barangay_id,month,year,dengue_ind1,dengue_ind2,dengue_ind3,dengue_ind4,dengue_ind5,dengue_ind6,dengue_ind7,dengue_ind8,dengue_ind9,dengue_ind10,dengue_ind11) values('".$getData[0]."','".$getData[1]."','".$getData[2]."','".$getData[3]."','".$getData[4]."','".$getData[5]."','".$getData[6]."','".$getData[7]."','".$getData[8]."','".$getData[9]."','".$getData[10]."')";
    
      // Perform a query, check for error
      if (!mysqli_query($con,$sql))
      {
         echo("Error description: " . mysqli_error($con));
      }
    

    因为列和传递的值不匹配 这个 mysqli_query($con,$sql) 函数返回 false 对于 $result 所以 if(!isset($result)) 行评估 并转到包含消息的else块 CSV File has been successfully Imported.