 |
ปัญหาจากการ import Excel to Mysql ค่ะ พอเลือกไฟล์ excel จากไฟล์ up.php แล้วส่งไปยัง import.php อ่ะค่ะ |
|
 |
|
|
 |
 |
|
พอเลือกไฟล์ excel จากไฟล์ up.php แล้วส่งไปยัง import.php อ่ะค่ะ มันจะ error ค่ะ
Warning: DOMDocument::load() [domdocument.load]: Start tag expected, '<' not found in file:///C:/WINDOWS/Temp/php7D.tmp, line: 1 in C:\AppServ\www\excel\import.php on line 18
Fatal error: Call to a member function getElementsByTagName() on a non-object in C:\AppServ\www\excel\import.php on line 19
*****ช่วยหน่อยนะคะ
ไฟล์ up.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
if ( $_FILES['file']['error'] ) {
die("upload error ");
}
//======Connect DB======================//
$m_host = "localhost";
$m_user = "root";
$m_pass = "123456";
$m_name = "mydata";
mysql_connect($m_host,$m_user,$m_pass);
mysql_select_db($m_name);
mysql_query("SET NAMES UTF8");
//======End Connect DB======================//
//======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 table_name (";
$sql .= " field_1, ";
$sql .= " field_2, ";
$sql .= " field_3, ";
$sql .= " field_4, ";
$sql .= " field_5 ";
$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();
?>
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2009-10-31 11:27:32 |
By :
gigza |
View :
48772 |
Reply :
9 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เคยลองอ่านไฟล์ xls ยังครับ แต่คิดว่าเอ๋น่าเคยทำแล้วแหล่ะ
พอดีเพิ่งลองศึกษาดู
download sourcecode:http://sourceforge.net/projects/phpexcelreader/<< อ่านไฟล์ excel
แก้ไข Excel/reader.php
//require_once 'Spreadsheet/Excel/Reader/OLERead.php'; //ไม่มีไฟล์นี้
require_once 'Excel/oleread.inc'; // เรียกใช้ไฟล์นี้แทน
jxlrwtest.xls// ไฟล์นี้เหมือนจะใช้ไม่ได้
download test.xls:http://www.beupload.com/download/?600861&A=532335 สร้าง test.xls ตัวอย่างให้ดู
หรือจะสร้างเองก็ได้ครับ
test.php// โค้ดเรียกใช้ฟังก์ชัน
test.php(PHP)
require_once 'Excel/reader.php';
$reader = new Spreadsheet_Excel_Reader();
$reader->setOutputEncoding("UTF-8");
$reader->read("test.xls");
for ($i = 1; $i <= $reader->sheets[0]["numRows"]; $i++)
{
for ($j = 1; $j <= $reader->sheets[0]["numCols"]; $j++)
{
print "\"".$reader->sheets[0]["cells"][$i][$j]."\",";
}
echo "\n";
}
หรือจะไปแก้ไขที่ example.php หรือ example2.php ก็ได้ครับ
ผลจากการรัน test.php
Code
"id","name","age", "101","xxx","1", "103","yyy","50", "105","zzz","21",
มาลองแทรกลง mysql กัน
table.sql
Code
create table a(
id int(4) ,
name varchar(20),
age int(4),
primary key(id)
);
excel2mysql.php
Code (PHP)
<?php
require_once 'Excel/reader.php';
$reader = new Spreadsheet_Excel_Reader();
$reader->setOutputEncoding("UTF-8");
$reader->read("test.xls");
for ($i = 2; $i <= $reader->sheets[0]["numRows"]; $i++)
{
$id =$reader->sheets[0]["cells"][$i][1];
$name=$reader->sheets[0]["cells"][$i][2];
$age=$reader->sheets[0]["cells"][$i][3];
mysql_query("insert into a (id,name,age) values('$id','$name','$age')") or die(mysql_error());
}
?>
อธิบาย
$i = 1 ตรงนี้เรารู้ว่ามันเริ่มที่แถวที่หนึ่ง คือ id name age แต่เราจะเริ่มแทรกข้อมูลที่แถวสอง $i จึงเริ่มต้นที่ 2
เรารู้ว่า id อยู่คอลัมน์ 1 name อยู่คอลัมน์2 และ age อยู่คอลัมน์3 เราก็เลยกำหนดไปเลยดังนี้
$id =$reader->sheets[0]["cells"][$i][1];
$name=$reader->sheets[0]["cells"][$i][2];
$age=$reader->sheets[0]["cells"][$i][3];
จากนั้นก็ query ข้อมูลที่ละแถวไปเรื่อยๆ
จะเห็นว่ามันไม่ยืดหยุ่นเท่าไรแต่ก็เป็นวิธีการแทรกลง mysql แบบง่ายๆครับ  
คงเป็นประโยชน์ให้เอ๋ไปดัดแปลงกับฟังก์ชันของตัวเองได้น่ะครับ
|
 |
 |
 |
 |
Date :
2009-10-31 15:24:26 |
By :
xbeginner01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวนี้ ทำให้อ่านภาษาไทยจาก excel ยังไงอ่ะครับ ทำยังไงก็ไม่ได้ซักที ?
|
 |
 |
 |
 |
Date :
2009-11-16 08:34:48 |
By :
leozx |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$data->setOutputEncoding('utf-8');
$data->setUTFEncoder('iconv');
ใส่ก่อนที่จะอ่าน ไฟล์ excel ครับ
|
 |
 |
 |
 |
Date :
2010-02-06 17:38:11 |
By :
kingnaka |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แสดงข้อมูลบนเว็บเป็นภาษาไทย
แต่ที่ mySql เป็นภาษายึกยือครับ
แก้ไขส่วนใหนครับ
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2010-06-25 18:32:54 |
By :
ผมเอง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
วันที่ที่เราอัพเดทเข้าไปแล้วมันมากกว่าวันที่ในไฟล์ excel 1 วันแก้ไขยังไงดีค่ะ
|
 |
 |
 |
 |
Date :
2010-10-21 18:32:38 |
By :
แพรว |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
cขอถาม คับ
บันทัดนี้คือไรคับ
require_once 'Excel/reader.php';
|
 |
 |
 |
 |
Date :
2011-01-19 11:57:18 |
By :
tester |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แจ่มค๊าบบ
|
 |
 |
 |
 |
Date :
2013-10-01 12:58:23 |
By :
topza1412 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ของผมขึ้นแบบนี้ ต้องแก้ยังไงดี
|
ประวัติการแก้ไข 2016-06-07 11:00:11
 |
 |
 |
 |
Date :
2016-06-07 10:59:24 |
By :
kongnoi001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|