คือผมเขียนโค้ด import ข้อมูลแบบ csv ไว้คับ ตอนแรกใช้งานได้แล้ว แต่มาตอนนี้ ใช้งานไม่ได้ เท่าที่ผมจำได้ ผมไม่ได้แก้ไขโค้ดตรงส่วนนั้นเลยคับ ผมเปลี่ยนแค่หน้าที่วางโค้ดอัพโหลดย้ายมาไว้หน้าใหม่ที่ตกแต่งเรียบร้อยแล้ว รบกวนช่วยแก้หน่อยนะคับ
...โค้ดช่อง Import ...
<div class="col-md-6" style="padding-left: 5px;">
<div class="box box-danger">
<div class="box-header with-border">
<h3 class="box-title">Import Employee</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<center><tr>
<td><input name="fileCSV" type="file" id="fileCSV" style="margin-right: -362px;margin-top: 16px;"></center></td>
<td><center><input name="btnSubmit" type="submit" id="btnSubmit" value="Submit" style="margin-right: -296px;margin-top: 50px;"></center></td>
</tr></center>
</div>
</div>
</div>
</div>
</div>
</div>
...โค้ด upload ไฟล์ CSV นำข้อมูลลงฐานข้อมูลคับ...
<?php
function importCSVtoMySQL($filename) {
//print_r($filename);exit;
setlocale(LC_ALL, 'th_TH.TIS-620');
//print_r(fopen("uploads/default/excel/".$filename, "r"));exit;
set_time_limit(10000);
ini_set("memory_limit", "512M");
if (($handle = fopen($filename, "r")) !== FALSE) {
# Set the parent multidimensional array key to 0.
$nn = 0;
//print_r(fgetcsv($handle, 1000, ","));exit;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
# Count the total keys in the row.
$c = count($data);
# Populate the multidimensional array.
for ($x = 0; $x < $c; $x++) {
//print_r($data[$x]);exit;
$csvarray[$nn][$x] = $data[$x];
}
//echo $nn."<br>";
$nn++;
}
//print_r($csvarray);
# Close the File.
fclose($handle);
}
//echo"<pre>";print_r($csvarray);exit;
return $csvarray;
}
$objConnect = mysql_connect("localhost","root","olf24382629") or die("Error Connect to Database"); // Conect to MySQL
$objDB = mysql_select_db("ASC");
$uploaddir = 'import_file/';
copy($_FILES['fileCSV']['tmp_name'],$uploaddir . $_FILES['fileCSV']['name']);
//echo ($_FILES['fileCSV']['name']); exit;
$uploadfile = $uploaddir . $_FILES['fileCSV']['name'];
//echo $uploadfile;
$objCSV = fopen($uploadfile, "r");
//echo $objCSV; exit;
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
//echo $objArr[10]."\n"; exit;
$data=split(" ", $objArr[9]);
$result = implode($data);
//echo $result;
$sqlString = "INSERT INTO `employee` (`EMP_ID`, `ASC_Code`,`Article_TH`, `EMP_Name_TH`, `EMP_Surname_TH`,`Article_EN`, `EMP_Name_EN`,`EMP_Surname_EN`, `Nickname`, `CID`, `Start_Date`, `Position`, `Site`, `Contact`, `Date_of_Birth`, `Address_No.`,`ASC_Mail`)
VALUES (NULL,
'".iconv('TIS-620', 'UTF-8',$objArr[1])."',
'".iconv('TIS-620', 'UTF-8',$objArr[2])."',
'".iconv('TIS-620', 'UTF-8',$objArr[3])."',
'".iconv('TIS-620', 'UTF-8',$objArr[4])."',
'".iconv('TIS-620', 'UTF-8',$objArr[5])."',
'".iconv('TIS-620', 'UTF-8',$objArr[6])."',
'".iconv('TIS-620', 'UTF-8',$objArr[7])."',
'".iconv('TIS-620', 'UTF-8',$objArr[8])."',
'".$result."',
'".iconv('TIS-620', 'UTF-8',$objArr[10])."',
'".iconv('TIS-620', 'UTF-8',$objArr[11])."',
'".iconv('TIS-620', 'UTF-8',$objArr[12])."',
'".iconv('TIS-620', 'UTF-8',$objArr[13])."',
'".iconv('TIS-620', 'UTF-8',$objArr[14])."',
'".iconv('TIS-620', 'UTF-8',$objArr[15])."',
'".iconv('TIS-620', 'UTF-8',$objArr[16])."')";
//echo $sqlString; exit;
$objQuery = mysql_query($sqlString);
}
fclose($objCSV);
echo "Upload & Import Done.";
?>
ขอสอบถามโค้ด import หน่อยค่าบบ
...โค้ดช่อง Import ...
<div class="col-md-6" style="padding-left: 5px;">
<div class="box box-danger">
<div class="box-header with-border">
<h3 class="box-title">Import Employee</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<center><tr>
<td><input name="fileCSV" type="file" id="fileCSV" style="margin-right: -362px;margin-top: 16px;"></center></td>
<td><center><input name="btnSubmit" type="submit" id="btnSubmit" value="Submit" style="margin-right: -296px;margin-top: 50px;"></center></td>
</tr></center>
</div>
</div>
</div>
</div>
</div>
</div>
...โค้ด upload ไฟล์ CSV นำข้อมูลลงฐานข้อมูลคับ...
<?php
function importCSVtoMySQL($filename) {
//print_r($filename);exit;
setlocale(LC_ALL, 'th_TH.TIS-620');
//print_r(fopen("uploads/default/excel/".$filename, "r"));exit;
set_time_limit(10000);
ini_set("memory_limit", "512M");
if (($handle = fopen($filename, "r")) !== FALSE) {
# Set the parent multidimensional array key to 0.
$nn = 0;
//print_r(fgetcsv($handle, 1000, ","));exit;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
# Count the total keys in the row.
$c = count($data);
# Populate the multidimensional array.
for ($x = 0; $x < $c; $x++) {
//print_r($data[$x]);exit;
$csvarray[$nn][$x] = $data[$x];
}
//echo $nn."<br>";
$nn++;
}
//print_r($csvarray);
# Close the File.
fclose($handle);
}
//echo"<pre>";print_r($csvarray);exit;
return $csvarray;
}
$objConnect = mysql_connect("localhost","root","olf24382629") or die("Error Connect to Database"); // Conect to MySQL
$objDB = mysql_select_db("ASC");
$uploaddir = 'import_file/';
copy($_FILES['fileCSV']['tmp_name'],$uploaddir . $_FILES['fileCSV']['name']);
//echo ($_FILES['fileCSV']['name']); exit;
$uploadfile = $uploaddir . $_FILES['fileCSV']['name'];
//echo $uploadfile;
$objCSV = fopen($uploadfile, "r");
//echo $objCSV; exit;
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
//echo $objArr[10]."\n"; exit;
$data=split(" ", $objArr[9]);
$result = implode($data);
//echo $result;
$sqlString = "INSERT INTO `employee` (`EMP_ID`, `ASC_Code`,`Article_TH`, `EMP_Name_TH`, `EMP_Surname_TH`,`Article_EN`, `EMP_Name_EN`,`EMP_Surname_EN`, `Nickname`, `CID`, `Start_Date`, `Position`, `Site`, `Contact`, `Date_of_Birth`, `Address_No.`,`ASC_Mail`)
VALUES (NULL,
'".iconv('TIS-620', 'UTF-8',$objArr[1])."',
'".iconv('TIS-620', 'UTF-8',$objArr[2])."',
'".iconv('TIS-620', 'UTF-8',$objArr[3])."',
'".iconv('TIS-620', 'UTF-8',$objArr[4])."',
'".iconv('TIS-620', 'UTF-8',$objArr[5])."',
'".iconv('TIS-620', 'UTF-8',$objArr[6])."',
'".iconv('TIS-620', 'UTF-8',$objArr[7])."',
'".iconv('TIS-620', 'UTF-8',$objArr[8])."',
'".$result."',
'".iconv('TIS-620', 'UTF-8',$objArr[10])."',
'".iconv('TIS-620', 'UTF-8',$objArr[11])."',
'".iconv('TIS-620', 'UTF-8',$objArr[12])."',
'".iconv('TIS-620', 'UTF-8',$objArr[13])."',
'".iconv('TIS-620', 'UTF-8',$objArr[14])."',
'".iconv('TIS-620', 'UTF-8',$objArr[15])."',
'".iconv('TIS-620', 'UTF-8',$objArr[16])."')";
//echo $sqlString; exit;
$objQuery = mysql_query($sqlString);
}
fclose($objCSV);
echo "Upload & Import Done.";
?>