รบกวนอีกครั้งครับเรืองการ import ไฟล์ excel เข้าฐานข้อมูล mysql โดยทำการ Browse มันติดปัญหาครับ
การ import ไฟล์ excel เข้าฐานข้อมูล mysql โดยทำการ Brows มันติดปัญหาครับ
ผมเริ่มจาก
เปลี่ยนนามสกุลจาก .xls เปลี่ยนเป็นนามสกุล .xml
อันนี้คือโด๊ดแบบฟอร์มครับ
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
if ( $_FILES['file']['error'] ) {
die("upload error ");
}
//======Connect DB======================//
$m_host = "localhost";
$m_user = "xxx";
$m_pass = "xxxx";
$m_name = "xxxxx";
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 user ====================================//
$sql = " INSERT INTO user (";
$sql .= " user_aluid, ";
$sql .= " user_name, ";
$sql .= " user_surname, ";
$sql .= " user_name_en, ";
$sql .= " user_surname_en ";
$sql .= " user_sex ";
$sql .= " user_grad ";
$sql .= " user_stu ";
$sql .= " user_st ";
$sql .= " user_address ";
$sql .= " ) VALUES ( ";
$sql .= " '$data1', ";
$sql .= " '$data2', ";
$sql .= " '$data3', ";
$sql .= " '$data4', ";
$sql .= " '$data5' ";
$sql .= " '$data6' ";
$sql .= " '$data7' ";
$sql .= " '$data8' ";
$sql .= " '$data9' ";
$sql .= " '$data10' ";
$sql .= " ) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To DB ====================================//
//==================Insert To user_contact ====================================//
$sql = " INSERT INTO user_contact (";
$sql .= " user_aluid, ";
$sql .= " user_tel, ";
$sql .= " user_mobile, ";
$sql .= " user_email, ";
$sql .= " ) VALUES ( ";
$sql .= " '$data1', ";
$sql .= " '$data2', ";
$sql .= " '$data3', ";
$sql .= " '$data4', ";
$sql .= " ) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To user_contact ====================================//
//==================Insert To user_job ====================================//
$sql = " INSERT INTO user_job (";
$sql .= " user_job, ";
$sql .= " user_office, ";
$sql .= " user_job_position, ";
$sql .= " user_email, ";
$sql .= " ) VALUES ( ";
$sql .= " '$data1', ";
$sql .= " '$data2', ";
$sql .= " '$data3', ";
$sql .= " ) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To user_job ====================================//
$row++;
}
mysql_close();
?>
หน้าจอที่ได้ครับ
ผลการรันครับ
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 'user_sex user_grad user_stu user_st user_address ) VALUES ( 'เธเธฒเธฃเธธเ' at line 1
เกิดจากอะไรเหรอครับ ขอบคุณครับTag : PHP, MySQL, CakePHP
Date :
2014-07-22 15:21:21
By :
khoson
View :
898
Reply :
6
ผิดตรงที่ ไม่มีคอมม่า(, ) คั่นระหว่างชื่อฟิลด์ครับ
Code (PHP)
user_surname_en ";
$sql .= " user_sex, ";
$sql .= " user_grad, ";
$sql .= " user_stu, ";
$sql .= " user_st, ";
$sql .= " user_address ";
การเขียนลักษณะนี้มีโอกาสผิดมาก แนะนำให้เขียนแบบนี้ครับ ดูง่ายกว่าเยอะ
Code (PHP)
//==================Insert To user ====================================//
$sql = " INSERT INTO user (
user_aluid
, user_name
, user_surname
, user_name_en
, user_surname_en
, user_sex
, user_grad
, user_stu
, user_st
, user_address
) VALUES (
'$data1'
, '$data2'
, '$data3'
, '$data4'
, '$data5'
, '$data6'
, '$data7'
, '$data8'
, '$data9'
, '$data10'
) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To DB ====================================//
Date :
2014-07-22 15:40:30
By :
arm8957
ครับผมขอบคุณครับ
Date :
2014-07-22 15:55:05
By :
khoson
ผมลองแก้ตามแล้วครับ พอ upload ไม่ฟ้องอะไรเลยครับแต่ข้อมูลไม่เข้าครับ
Code (PHP)
<?php
if ( $_FILES['file']['error'] ) {
die("upload error ");
}
//======Connect DB======================//
$m_host = "localhost";
$m_user = "staff";
$m_pass = "itkmutnb";
$m_name = "alumni";
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 user ====================================//
$sql = " INSERT INTO user (
user_aluid
, user_name
, user_surname
, user_name_en
, user_surname_en
, user_sex
, user_grad
, user_stu
, user_st
, user_address
) VALUES (
'$data1'
, '$data2'
, '$data3'
, '$data4'
, '$data5'
, '$data6'
, '$data7'
, '$data8'
, '$data9'
, '$data10'
) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To DB ====================================//
//==================Insert To user_contact ====================================//
$sql = " INSERT INTO user_contact (
, user_aluid
, user_tel
, user_mobile
, user_email
) VALUES (
, '$data1'
, '$data2'
, '$data3'
, '$data4'
) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To user_contact ====================================//
//==================Insert To user_job ====================================//
$sql = " INSERT INTO user_job (
, user_job
, user_office
, user_job_position
, sql .= " user_email, ";
) VALUES (
, '$data1'
, '$data2'
, '$data3'
) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To user_job ====================================//
$row++;
}
mysql_close();
?>
มันเกิดจากการ error หรือเปล่าครับ
Date :
2014-07-22 17:55:57
By :
khoson
อันนี้ไม่แน่ใจว่าเกิดจากปัญหาตรงนี้หรือเปล่าครับ คือผมมีฟิวจาก database ตามนี้ครับ
จากฐานข้อมูลผมมาใส่ข้อมูลใน excel
ผมเลยสงสัย code
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++;
ตรงนี้ไม่ครบหรือเปล่าครับ รบกวนที่ครับ ขอบคุณครับ
Date :
2014-07-22 18:17:16
By :
khoson
ในการใส่คอมม่า(,) ในส่วนของชื่อฟิลด์ กับชื่อตัวแปร ในกรณีที่ใส่ไว้ข้างหน้า(แบบที่ผมถนัด) ตัวแรกไม่ต้องใส่ครับ แต่ถ้าใส่ข้างหลังชื่อฟิลด์ และชื่อตัวแปร อันนั้นใส่ทุกตัวครับ ลองปรับถามถนัดครับ ในส่วนของโครงสร้างโค้ด SQL ไม่น่าผิดแล้ว ทีนี้ก็เรื่องการดึงค่าจาก excel แล้วละครับ อันนี้ก็ลอง bug เอาเองนะครับ
Code (PHP)
//==================Insert To user_contact ====================================//
$sql = " INSERT INTO user_contact (
user_aluid
, user_tel
, user_mobile
, user_email
) VALUES (
'$data1'
, '$data2'
, '$data3'
, '$data4'
) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To user_contact ====================================//
//==================Insert To user_job ====================================//
$sql = " INSERT INTO user_job (
user_job
, user_office
, user_job_position
, sql .= " user_email, ";
) VALUES (
'$data1'
, '$data2'
, '$data3'
) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To user_job ====================================//
Date :
2014-07-22 23:25:02
By :
arm8957
อีกทีน่ะครับ
Code (PHP)
<?php
if ( $_FILES['file']['error'] ) {
die("upload error ");
}
//======Connect DB======================//
$m_host = "localhost";
$m_user = "xxx";
$m_pass = "xxx";
$m_name = "xxx";
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 user ====================================//
$sql = " INSERT INTO user (
user_aluid
, user_name
, user_surname
, user_name_en
, user_surname_en
, user_sex
, user_grad
, user_stu
, user_st
, user_address
) VALUES (
'$data1'
, '$data2'
, '$data3'
, '$data4'
, '$data5'
, '$data6'
, '$data7'
, '$data8'
, '$data9'
, '$data10'
) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To DB ====================================//
//==================Insert To user_contact ====================================//
$sql = " INSERT INTO user_contact (
user_aluid
, user_tel
, user_mobile
, user_email
) VALUES (
'$data1'
, '$data2'
, '$data3'
, '$data4'
) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To user_contact ====================================//
//==================Insert To user_job ====================================//
$sql = " INSERT INTO user_job (
user_job
, user_office
, user_job_position
, sql .= " user_email, ";
) VALUES (
'$data1'
, '$data2'
, '$data3'
) ";
mysql_query($sql) or die(mysql_error());
//==================End Insert To user_job ====================================//
$row++;
}
mysql_close();
?>
พอผมรันแล้วไม่แสดงผลอะไรเลยครับ error ก็ไม่แจ้งครับมันน่าจะเกิดจากอะไรเหรอครับ ขอบคุณครับ
ประวัติการแก้ไข 2014-07-23 10:07:32
Date :
2014-07-23 08:53:14
By :
khoson
Load balance : Server 04