我在将csv文件导入到mysql时遇到问题,我对该主题不熟悉,这是我要用于将csv文件导入到mysql的代码,我不知道为什么它仍然会给出错误。我需要有人帮助我在此处检查代码中的错误。

import.php

    <script>
       $(document).ready(function() {

                $('#submit').live('click', function(){
                  $("#imageform").submit();

                });
            });




            </script>

    </head>
     <body>
     <div id="imp1">
     <div id="import">Importing into Current Server</div>
     <form id="imageform" method="post" enctype="multipart/form-data" action="validate.php">
     <div class="far">
        <label id="sel">Upload File</label>
            <div class="far1">
            <div id="preview"></div>
    <input type="file" name="filename" id="filename" />
    </div>
    <div class="clear"></div>
    </div>
    <div><a href="#"><input id="submit" type="submit" name="submit" value="submit" /><span></span></a></div></form>
    </div>
   </body>


mysql.php

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "sample";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>


validate.php

     include "mysql.php"; //Connect to Database

        $deleterecords = "TRUNCATE TABLE customer";
        mysql_query($deleterecords);

        //Upload File
        if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
        {
             $fname = $_FILES['filename']['name'];

             $chk_ext = explode(".",$fname);

             if(strtolower($chk_ext[1]) == "csv")
             {

                 $filename = $_FILES['filename']['name'];
                 $handle = fopen($filename, "r");

                 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
                 {
                    $sql = "INSERT into customer(item2,item3,item4,item5,item6,item7,item8,item9,item10,item11)
    values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')";
//item1 is AI
                    mysql_query($sql) or die(mysql_error());
                 }

                 fclose($handle);
                 echo "Successfully Imported";
             }
             else
             {
                 echo "Invalid File";
             }
        }


        ?>


谢谢你的帮助 。

最佳答案

可能是由于重复的函数名称,不匹配的引号,缺少;引起的。等等

在这种情况下,可以通过替换$ fname = $ _FILES ['filename'] ['name']来解决问题。 -在第9行,其中$ filename = $ _FILES ['filename'] ['name'];在第16行

09-11 19:46
查看更多