2012-11-28 27 views
0

我想使用csv文件來更新mysql scv表。如何編碼?我沒有做這項工作的經驗。如何上傳csv文件並更新mysql db?

<p>please select a scv file to upload</p> 
<form action="index.php" method="post"> 
    <input type="file" name="scv" /> 
    <input type="submit" value="submit" /> 
</form> 
<?php 
    mysql_connect('localhost','root','admin'); 
    mysql_select_db('linjuming'); 
    // how to upload a scv file and insert or update the "csv" table? 

?> 

enter image description here

+0

[PHP/MYSQL Upload,import .csv file to mysql-process-table design]可能重複(http://stackoverflow.com/questions/10657204/php-mysql-upload-import-csv-file-to -mysql-process-table-design) – Kermit

+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護,[棄用過程](http://j.mp/Rj2iVR)已經開始。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –

回答

1

上傳文件:

<form action="upload_target.php" method="post" enctype="multipart/form-data"> 
<label for="file">Filename:</label> 
<input type="file" name="file" id="file"><br> 
<input type="submit" name="submit" value="Submit"> 
</form> 

upload_target.php

$分機= PATHINFO($ _ FILES [ '文件'] [」名稱'],PATHINFO_EXTENSION);

if ($ext == "csv" && $_FILES["file"]["error"] == 0) 
{ 
    $target = "upload/" . $_FILES["file"]["name"]; 
    move_uploaded_file($_FILES["file"]["tmp_name"], $target); 

    if (($handle = fopen($target, "r")) !== FALSE) 
    { 
     while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) 
     { 
      print_r($data); 
     } 

     fclose($handle); 
    } 
} 

非常基本且很少檢查/驗證。 print_r($data)包含您現在可以在數據庫中插入的一行csv。

但是,我建議使用PDO或MySQLi來完成該任務,因爲PHP的mysql函數將來會被棄用。

1

有這樣幾個部分:

首先,你的表格必須有加密類型設置,如下所示:

<form enctype="multipart/form-data" action="index.php" method="post"> 

否則,將不接受文件上傳。

一旦你這樣做了,那麼你可以使用$_FILES變量訪問文件。該文件已被上傳後,那麼你可以像這樣訪問:

if (isset($_FILES["scv"])) { 
    $file = $_FILES["scv"]; 
    $file_name = $file["name"]; 
    $ext = pathinfo($file_name, PATHINFO_EXTENSION); 
    if ($ext!="CSV" && $ext!="TXT") { 
     die('The file must be csv or txt format.'); 
    } 
    $saveto_path_and_name = '/path/to/file.csv'; // Where you want to save the file 
    move_uploaded_file($file["tmp_name"], $saveto_path_and_name); 
} 

一旦你保存文件,然後你可以打開它,將其導入。這不是容易做到的,但這裏的一些底漆代碼:

// Open the file for reading 
$handle = @fopen($saveto_path_and_name, "r") or die(__("Unable to open uploaded file!", "inventory")); 
// Grab the first row to do some checks 
$row = fgets($inv_file, 4096); 
// See if it's comma or tab delimited 
if (stripos($inv_row, "\t")) { 
    $sep = "\t"; 
} else { 
    $sep = ","; 
} 

while (! feof($handle)) { 
    $rowcount = 0; 
    // Get the individual fields 
    $inv_fields = explode($sep, $inv_row); 
    $fields = array(); 
    // Iterate through the fields to do any sanitization, etc. 
    foreach ($inv_fields as $field) { 
     // Highly recommended to sanitize the variable $field here.... 
     $fields[] = $field; 
     $rowcount++; 
} 
    // This is where you would write your query statement to insert the data 
    // This is just EXAMPLE code. Use the DB access of your choice (PDO, MySQLi) 
    $sql = vsprintf('INSERT INTO `table` (`column`, `column2`, ...) VALUES (%s, %d, ...)', $fields); 
    // Get the next row of data from the file 
    $row = fgets($inv_file, 4096); 
}