2014-01-11 104 views
0

是否可以將excel文件上傳到我的php myadmin。我知道我的phpmyadmin數據庫表的所有字段名稱和我的Excel表格字段應該是相同的。但我沒有找到適合本主題的解決方案。我沒有發現我的Excel文件代碼的導入。而在這個困惑。通過文件上傳使用php導入excel文件到php myadmin使用php

下面是我嘗試了代碼:

<form action="upload.php"> 
     <input type="file" name="txtFile" id="eskal" /></br> 
<input type="submit" name="Import" value="Update Database" /> </b> 

upload.php的:

<?php 
if(isset($_POST["Import"])) 
{ 
$host="localhost"; // Host name. 
$db_user="root"; 
$db_password=""; 
$db='test'; // Database name. 
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error()); 
mysql_select_db($db) or die (mysql_error()); 

echo $filename=$_FILES["file"]["tmp_name"]; 
//echo $ext=substr($filename,strrpos($filename,"."),(strlen($filename)-strrpos($filename,"."))); 


if($_FILES["file"]["size"] > 0) 
{ 

    $file = fopen($filename, "r"); 
     while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) 
     { 
      print_r($emapData); 
      $sql = "INSERT into import(name,address,email,password) values('$emapData[0]','$emapData[1]')"; 
      mysql_query($sql); 
     } 
     fclose($file); 
     echo "CSV File has been successfully Imported"; 
} 
else 
echo "Invalid File:Please Upload CSV File"; 

} 
?> 

我在我的upload.php的代碼錯了我KNW。所以,如果有人可以幫我在這裏的代碼。 任何形式的幫助表示讚賞。 在此先感謝

回答

0

我找到了我的解決方案。現在allz工作得很好。我發佈我的完整代碼。

upload.php的:

<html> 
<body style=" 
    background-color: rgb(128, 151, 185); 
"> 

<form action="import_file.php" method="post" 
     enctype="multipart/form-data"> 
<table> 
    <tr> 
     <td> 
      Filename: 
     </td> 
     <td> 
      <input type="file" name="file" id="file"> 
     </td> 
    </tr> 
    <tr> 
     <td colspan="2" align="right"> 
      <input type="submit" name="submit" value="Submit"> 
     </td> 
    </tr> 
</table> 
</form> 

</body> 
</html> 

import_file.php:

<?php 
if ($_FILES["file"]["error"] > 0) 
{ 
    echo "Error: " . $_FILES["file"]["error"] . "<br>"; 
} 
else 
{ 
    echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
    echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
    echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br>"; 
    //echo "Stored in: " . $_FILES["file"]["tmp_name"]; 
    $a=$_FILES["file"]["tmp_name"]; 
    //echo $a; 

    $connect = mysql_connect('localhost','root',''); 
if (!$connect) { 
die('Could not connect to MySQL: ' . mysql_error()); 
} 
//your database name 
$cid =mysql_select_db('test',$connect); 

// path where your CSV file is located 
//define('CSV_PATH','C:/xampp/htdocs/'); 
//<!-- C:\xampp\htdocs --> 
// Name of your CSV file 
$csv_file = $a; 

if (($getfile = fopen($csv_file, "r")) !== FALSE) { 
     $data = fgetcsv($getfile, 1000, ","); 
    while (($data = fgetcsv($getfile, 1000, ",")) !== FALSE) { 
    //$num = count($data); 
     //echo $num; 
     //for ($c=0; $c < $num; $c++) { 
      $result = $data; 
      $str = implode(",", $result); 
      $slice = explode(",", $str); 

      $col1 = $slice[0]; 
      $col2 = $slice[1]; 
      $col3 = $slice[2]; 
      $col4 = $slice[3]; 

$query = "INSERT INTO persons(id, name, email ,contacts) VALUES('".$col1."','".$col2."','".$col3."','".$col4."')"; 
$s=mysql_query($query, $connect); 
} 
} 
echo "<script>alert('Record successfully uploaded.');window.location.href='edit_table.php';</script>"; 
//echo "File data successfully imported to database!!"; 
mysql_close($connect); 
} 
?> 

以上是充分的工作代碼。