|
 |
|
ช่วยหน่อยครับ import ข้อมูลใน excel ไม่เข้าใน database |
|
 |
|
|
 |
 |
|
ผมจะ import ข้อมูลในไฟล์ excel เข้า database ครับ เเต่ติดตรงที่มันไม่ insert ข้อมูลเข้าไป ช่วยดูให้หน่อยครับว่าผิดตรงส่วนไหน ขอบคุณครับ
Code (PHP)
require_once 'phpexcel/PHPExcel/Classes/PHPExcel.php';
require 'phpexcel/PHPExcel/Classes/PHPExcel/IOFactory.php';
#$inputFileName = $path_src . $data['name'];
$inputFileName = $_FILES['pic']['tmp_name'];
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$headingsArray = $objWorksheet->rangeToArray('A1:' . $highestColumn . '1', null, true, true, true);
$headingsArray = $headingsArray[1];
$r = -1;
$namedDataArray = array();
for ($row = 2; $row <= $highestRow; ++$row) {
$dataRow = $objWorksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, null, true, true, true);
if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
++$r;
foreach ($headingsArray as $columnKey => $columnHeading) {
$namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
}
}
}
foreach ($namedDataArray as $resx) {
mysql_query(" INSERT INTO " . $prefix . "test (first_name,last_name) VALUES('" . $resx['first_name'] . "','" . $resx['last_name'] . "')");
}
Tag : PHP, HTML5, JavaScript, jQuery

|
|
 |
 |
 |
 |
Date :
2020-07-03 10:54:53 |
By :
Antinew |
View :
634 |
Reply :
7 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. echo $resx['first_name']; ใน foreach มาดูหน่อยครับ มีข้อมูลหรือไม่
2. ขอดู database table structure (หรือ ชื่อ field / column header)
|
 |
 |
 |
 |
Date :
2020-07-03 11:45:07 |
By :
PhrayaDev |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$namedDataArray อาจไม่มีข้อมูล
เพื่อความเข้าใจตรงกัน
comment line 30-32
วางโค้ด
Code (PHP)
print_r($namedDataArray);
echo "<br><br>Test in foreach:<br>";
foreach ($namedDataArray as $resx) {
echo $resx['first_name'];
}
แล้วนำผลมาโพสต์ครับ
|
 |
 |
 |
 |
Date :
2020-07-03 13:29:10 |
By :
PhrayaDev |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไล่เช็คย้อนกลับไปทีละอย่างครับ ตั้งแต่ $dataRow
Code (PHP)
for ($row = 2; $row <= $highestRow; ++$row) {
$dataRow = $objWorksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, null, true, true, true);
print_r($dataRow);
echo "<br>";
}
ถ้ายังไม่มีอีกแสดงว่า เป็นที่ file source ($inputFileName)
|
ประวัติการแก้ไข 2020-07-03 16:41:15
 |
 |
 |
 |
Date :
2020-07-03 14:59:46 |
By :
PhrayaDev |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|