|
 |
|
ช่วยดูให้ทีครับ เรื่อง Import Excel เข้าฐานข้อมูล ผมจะอธิบายทีละ Step นะครับ Database |
|
 |
|
|
 |
 |
|
ผมจะอธิบายทีละ Step นะครับ
Database ชื่อ Ex1 มีฟิลด์ a b c d ครับ
รับค่าจากที่นี้
frm_upload.php
<form action="upload.php" method="post" enctype="multipart/form-data" >
แนบไฟล์
<input type="file" name="filesupload" size = "30"/>
<input type="submit" name="submit" value="ทำรายการ">
</form>
ทำการ copy file ที่ upload มา
upload.php
<?
if ($filesupload == null)
{
?>
<center><font size="15" color="red">กรุณาตรวจสอบข้อมูลใหม่</font><center>
<center><font size="5" color="blue"><a href= "frm_add_doc.php">กลับไปหน้าเอกสาร </a>หรือรอประมาณ 5 วินาที</font><center>
<meta http-equiv="refresh" content="5; URL=frm_upload.php">
<?
exit();
}else{
if(copy($_FILES["filesupload"]["tmp_name"],"file_data/".$_FILES["filesupload"]["name"]))
{
//echo "Copy/Upload Complete";
}
$filename=$_FILES["filesupload"]["name"];
$pathfile="file_data/";
$path="$pathfile"."$filename";
}
?>
<meta http-equiv="refresh" content="3; URL=phpImportExcelToDatabase.php?fname=<?echo $filename; ?>">
อ่านข้อมูล จาก Excel แล้วนำเข้าฐานข้อมูล SQL
phpImportExcelToDatabase.php
<html>
<head>
<title>ThaiCreate.Com PHP(COM) Excel.Application Tutorial</title>
</head>
<body>
<? echo $fname;
//*** Get Document Path ***//
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
$OpenFile = "file_data/$fname";
$OpenFile;
//*** Create Exce.Application ***//
$xlApp = new COM("Excel.Application");
$xlBook = $xlApp->Workbooks->Open($strPath."/".$OpenFile);
//$xlBook = $xlApp->Workbooks->Open(realpath($OpenFile));
$xlSheet1 = $xlBook->Worksheets(1);
//*** Insert to MySQL Database ***//
$objConnect = mysql_connect("localhost","root","065863786") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
for($i=1;$i<=4;$i++){
If(trim($xlSheet1->Cells->Item($i,1)) != "")
{
$strSQL = "";
$strSQL .= "INSERT INTO ex1 ";
$strSQL .= "(a,b,c,d) ";
$strSQL .= "VALUES ";
$strSQL .= "('".$xlSheet1->Cells->Item($i,1)."','".$xlSheet1->Cells->Item($i,2)."' ";
$strSQL .= ",'".$xlSheet1->Cells->Item($i,3)."','".$xlSheet1->Cells->Item($i,4)."' ";
mysql_query($strSQL);
echo $i;
}
}
//*** Close MySQL ***//
@mysql_close($objConnect);
//*** Close & Quit ***//
$xlApp->Application->Quit();
$xlApp = null;
$xlBook = null;
$xlSheet1 = null;
?>
Data Import/Inserted.
</body>
</html>
จากโค้ดทั้งหมด ผลปรากฏว่า สามารถ อัพโหลดได้ อ่านไฟล์ได้ แต่ข้อมูลที่อ่านมันไม่ Insert เข้าฐานเลย ไม่มี Error อะไรด้วย ครับ ไม่ทราบว่าจะต้องแก้ไขอะไรหรือเปล่าครับ
Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2010-03-20 13:04:00 |
By :
drakvender |
View :
1272 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลอง echo $strSQL; ครับ
mysql_query($strSQL) or die (mysql_error());
echo $strSQL;
|
 |
 |
 |
 |
Date :
2010-03-20 15:36:13 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ทราบ ว่ามันขึ้น แบบนี้ ผิดพลาด ตรงไหน หรือ ครับ
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
|
 |
 |
 |
 |
Date :
2010-03-21 16:51:19 |
By :
drakvender |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งง จริง ๆๆ เลย ยังทำไม่ได้เลย ทำได้แต่ CSV งะ
|
 |
 |
 |
 |
Date :
2010-03-23 23:01:01 |
By :
drakvender |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|