ช่วยดูโค้ดวิธีการนำข้อมูลจากไฟล์ Excel Import เข้าสู่ฐานข้อมูล MySql เวลารันฟ้อง error .... ค่ะ
ไฟล์ upload.php
Code (PHP)
<!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>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="import.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input name="file" type="file" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
ไฟล์ import.php
Code (PHP)
<?php session_start();
include("connect.php");
?>
<!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>Untitled Document</title>
</head>
<body>
<?php
if ( $_FILES['file']['error'] ) {
die("upload error ");
}
//======Get data from Excel======================//
$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
$rows = $dom->getElementsByTagName( 'Row' );
$row = 0;
foreach ($rows as $temp) {
$col = 0;
if($row==0) {
$row++; continue;
}
$cells = $temp->getElementsByTagName('Cell');
foreach( $cells as $cell ) {
if($col==0) $data1 = $cell->nodeValue;
if($col==1) $data2 = $cell->nodeValue;
if($col==2) $data3 = $cell->nodeValue;
if($col==3) $data4 = $cell->nodeValue;
if($col==4) $data5 = $cell->nodeValue;
$col++;
}
//======End Get data from Excel======================//
//==================Insert To DB ====================================//
$sql = " INSERT INTO member (";
$sql .= " id_cade, ";
$sql .= " password, ";
$sql .= " prefix_id, ";
$sql .= " fname , ";
$sql .= " lname ";
$sql .= " ) VALUES ( ";
$sql .= " '$data1', ";
$sql .= " '$data2', ";
$sql .= " '$data3', ";
$sql .= " '$data4', ";
$sql .= " '$data5' ";
$sql .= " ) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To DB ====================================//
$row++;
}
mysql_close();
?>
</body>
</html>
พอรันแล้วฟ้องว่า
Warning: DOMDocument::load() [domdocument.load]: Start tag expected, '<' not found in file:///C:/WINDOWS/Temp/phpAA67.tmp, line: 1 in E:\AppServ\www\payrollreport\import.php on line 29
Fatal error: Call to a member function getElementsByTagName() on a non-object in E:\AppServ\www\payrollreport\import.php on line 30
Tag : PHP, MySQL
Date :
2013-05-17 13:56:14
By :
eii-mian
View :
753
Reply :
2
ขอแปะใหม่นะครับ อ่านยากแท้
ไฟล์ upload.php
Code (PHP)
<!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>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="import.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input name="file" type="file" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
ไฟล์ import.php
Code (PHP)
<?php session_start();
include("connect.php");
?>
<!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>Untitled Document</title>
</head>
<body>
<?php
if ( $_FILES['file']['error'] ) {
die("upload error ");
}
//======Get data from Excel======================//
$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
$rows = $dom->getElementsByTagName( 'Row' );
$row = 0;
foreach ($rows as $temp) {
$col = 0;
if($row==0) {
$row++; continue;
}
$cells = $temp->getElementsByTagName('Cell');
foreach( $cells as $cell ) {
if($col==0) $data1 = $cell->nodeValue;
if($col==1) $data2 = $cell->nodeValue;
if($col==2) $data3 = $cell->nodeValue;
if($col==3) $data4 = $cell->nodeValue;
if($col==4) $data5 = $cell->nodeValue;
$col++;
}
//======End Get data from Excel======================//
//==================Insert To DB ====================================//
$sql = " INSERT INTO member (";
$sql .= " id_cade, ";
$sql .= " password, ";
$sql .= " prefix_id, ";
$sql .= " fname , ";
$sql .= " lname ";
$sql .= " ) VALUES ( ";
$sql .= " '$data1', ";
$sql .= " '$data2', ";
$sql .= " '$data3', ";
$sql .= " '$data4', ";
$sql .= " '$data5' ";
$sql .= " ) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To DB ====================================//
$row++;
}
mysql_close();
?>
</body>
</html>
Date :
2013-05-17 13:59:23
By :
itpcc
ใช้ตัวนี้แทนครับ
สร้าง Excel (xls,xlsx) ไฟล์ด้วย PHPExcel และการส่งออก Export จากฐานข้อมูล MySQL Database
Date :
2013-05-18 07:45:44
By :
mr.win
Load balance : Server 05