2013-10-01 99 views
0

我有問題導入csv文件到mysql,我不熟悉這個主題,這裏是我想用於導入csv文件到MySQL的代碼,我沒有一個主意爲什麼它仍然會出錯。我需要有人幫助我檢查我的代碼在這裏出了什麼問題。錯誤導入csv到MySQL php jquery

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"; 
      }  
     } 


     ?> 

感謝您的幫助。

+1

它給了什麼錯誤? –

+0

服務器錯誤。網站在檢索localhost/import/import/validate.php時遇到錯誤。它可能因維護而關閉或配置不正確。 <<當我查看錶格時,我設置爲自動增量的item1中的值正在增加。現在我很困惑 – art

+1

這是一個非常普遍的錯誤 - 例如,它可能是由重複的函數名稱,不匹配的引號,缺少';'等等 - 查看你的日誌文件,看看實際的錯誤是什麼。 –

回答

1

它可能是由重複的函數名稱,不匹配的引號,缺少;等等。

在這種情況下,通過替換$ fname = $ _FILES ['filename'] ['name']; - 在$ filename = $ _FILES ['filename'] ['name'];在第16行