reset and begin
10/09/2010

import file exel thành mysql trong php

b1: giả sử có file bangdiem.csv trên Desktop (bắtr buộc phải lưu dưới dạng .csv để lưu dữ liệu với phân cách là dấu phẩy, có thể dùng wordpad mổ ra coi kiểm chứng)

bangdiem.csv

[caption id="" align="alignnone" width="318" caption="http://img705.imageshack.us/img705/8397/bangdiem.jpg"]http://img705.imageshack.us/img705/8397/bangdiem.jpg[/caption]

mysql
http://img69.imageshack.us/img69/2507/bangdiemmysql.jpg
b2: tạo trang demo.php như sau

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Trung Tam Nhat Nghe</title>
</head>

<body>
<?
$db = mysql_connect("localhost", "root", "root") or die("Could not connect.");

if(!$db)

die("no db");

if(!mysql_select_db("quanlydiem",$db))

die("No database selected.");

if(isset($_GET['submit']))

{

$filename=$_GET['filename'];

$handle = fopen("$filename", "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$import="INSERT into  bangdiem(hoten,toan,van) values('$data[0]',$data[1],$data[2])";

mysql_query($import) or die(mysql_error());
}

fclose($handle);

print "Import done";

}

else

{
print "<form action='demo.php' method='get'>";

print "Type file name to import:<br>";

print "<input type='file' name='filename' size='20' width=40 value='bangdiem.csv'><br>";

print "<input type='submit' name='submit' value='submit'></form>";
}


?>
</body>
</html>


kết quả http://img686.imageshack.us/img686/316/ketqua.jpg

Bloger Comments
G+ Comments
Comments FaceBook

No comments:

List

Profiles Information


About me : Nothing is 1 vài thứ - 1985

Places I've Lived : I Hà Nội

Home Page : http://www.shimivn.blogspot.com/

Think : 1:1000000000

Languages spoken : Vietnamese,English.

Mobile : sony C2305

dell : i3-Ram 3GB- HDD 250GB .