2015-04-05 57 views
0

我希望用戶將csv文件上傳到mysql數據庫。當我運行upload.php文件時,這是我在我的testcsv表中看到的唯一更改,它是增加的表ID。沒有數據輸入到表格中。我使用excel 2013,並將文件保存爲CSV(MS-DOS)。我收到了一堆錯誤,如下列:將csv文件上傳到mysql數據庫。 upload.php文件中的錯誤

Notice: Undefined index: lec_name in C:\XAMPP\htdocs\statistics\upload.php on line 48 

Notice: Undefined index: dept_name in C:\XAMPP\htdocs\statistics\upload.php on line 49 

enter image description here

upload.php的

<html> 
<head> 
<link rel="stylesheet" type="text/css" href="../../statistics/style.css"> 
<body> 
    <div class="nav"> 
     <ul> 
     <li><a href="../../statistics/principalForm.php">Principal</a></li> 
     <li><a href="../../statistics/ac_directorForm.php">Academic Director</a></li> 
     <li><a href="../../statistics/lecturerForm.php">Lecturer</a></li> 
     <li> 
     <a href="#">Admin</a> 
      <ul> 
      <li><a href="../../statistics/adminFormLecturer">Save Lecturer Scores</a></li> 
      <li><a href="../../statistics/adminFormServices">Save Services Scores</a></li> 
      </ul> 
     </li> 
     <li><a href="../../statistics/logout.php">Logout</a></li> 

     </ul> 
    </div> 

    <br /> 
    <br /> 
    <br /> 

    <div id="myform"> 
     <?php 

     include 'connect.php'; 

     $deleterecords = "TRUNCATE TABLE testcsv"; //empty the table of its current records 
     mysql_query($deleterecords); 

     //Upload File 
     if (isset($_POST['submit'])) { 
     if (is_uploaded_file($_FILES['filename']['tmp_name'])) { 
     echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>"; 
     echo "<h2>Displaying contents:</h2>"; 
     readfile($_FILES['filename']['tmp_name']); 
     } 

     //Import uploaded file to Database 
     $handle = fopen($_FILES['filename']['tmp_name'], "r"); 
     while(($data = fgetcsv($handle, 1000, ",")) !== FALSE){ 

     $value1=$_POST['lec_name']; 
     $value2=$_POST['dept_name']; 

     $import="INSERT into testcsv(lec_name,dept_name) values('$value1','$value2')"; 
     mysql_query($import) or die(mysql_error()); 
    } 

     fclose($handle); 

     print "Import done"; 

     } 
     else { 

     print "Upload new csv by browsing to file and clicking on Upload<br />\n"; 
     print "<form enctype='multipart/form-data' action='upload.php' method='post'>"; 
     print "File name to import:<br /><br />\n"; 
     print "<input size='50' type='file' name='filename'><br /><br />\n"; 
     print "<input type='submit' name='submit' value='Upload'></form>"; 

     } 
     ?> 

     </div> 
     </body> 
     </head> 
     </html> 
+0

在你的表單中,沒有一些代碼像 2015-04-05 08:38:15

+0

@Ashot Khanamiryan是的,我知道我只是想要上傳文件不要輸入lec_name和dept_name。 – 2015-04-05 08:40:04

+0

請發佈您的csv文件的示例數據。 – 2015-04-05 08:45:11

回答

1

更改代碼

$value1=$_POST['lec_name']; 
$value2=$_POST['dept_name']; 

有:

$value1=$data[0]; 
    $value2=$data[1]; 
+0

在一行中,非常感謝你的作品很棒! – 2015-04-05 09:09:00