|
 |
|
สอบถามเรื่อง code อัพโหลดไฟล์ excel ลงฐานข้อมูลค่ะ ไม่ทราบ่วาเขียนยังไงคะ |
|
 |
|
|
 |
 |
|
แนะนำให้เป็น CSV จะดีกว่าน่ะครับ อ่านได้ดีกว่าเร็วกว่าด้วยครับ
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & CSV To MySQL</title>
</head>
<body>
<?
copy($_FILES["fileCSV"]["tmp_name"],$_FILES["fileCSV"]["name"]); // Copy/Upload CSV
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database"); // Conect to MySQL
$objDB = mysql_select_db("mydatabase");
$objCSV = fopen($_FILES["fileCSV"]["name"], "r");
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$strSQL = "INSERT INTO customer ";
$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";
$strSQL .="VALUES ";
$strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."' ";
$strSQL .=",'".$objArr[3]."','".$objArr[4]."','".$objArr[5]."') ";
$objQuery = mysql_query($strSQL);
}
fclose($objCSV);
echo "Upload & Import Done.";
?>
</table>
</body>
</html>
Go to : PHP Upload and Convert/Import CSV To MySQL Database
Go to : PHP Import Excel to MySQL Database (Excel.Application)
|
 |
 |
 |
 |
Date :
2011-05-12 21:38:49 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่วินคะ เอา code มาให้ดู ส่วนที่เป็นตัวอัพโหลด ชื่อ
import.php
Code (PHP)
<form method=post action=Upload.php>
Upload : <input type=file name=fileCSV size=25><input type=submit value=Upload>
</form>
ส่งไปที่ไฟล์ Upload.php
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & CSV To MySQL</title>
</head>
<body>
<?
copy($_FILES["fileCSV"]["tmp_name"],$_FILES["fileCSV"]["name"]); // Copy/Upload CSV
$objConnect = mysql_connect("localhost","root","123456") or die("Error Connect to Database"); // Conect to MySQL
$objDB = mysql_select_db("excel");
$objCSV = fopen($_FILES["fileCSV"]["name"], "r");
while (($objArr = fgetcsv($objCSV, 5, ",")) !== FALSE) {
$strSQL = "INSERT INTO customer ";
$strSQL .="(cusid,name,email,code,budget,used) ";
$strSQL .="VALUES ";
$strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."' ";
$strSQL .=",'".$objArr[3]."','".$objArr[4]."','".$objArr[5]."') ";
$objQuery = mysql_query($strSQL);
}
fclose($objCSV);
echo "Upload & Import Done.";
?>
</table>
</body>
</html>
ฐานข้อมูล (excel) และตาราง (customer) ก็สร้าง cusid,name,email,code,budget,used แล้ว
รันแล้ว Error ว่า
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\AppServ\www\upload\Upload.php on line 13
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\AppServ\www\upload\Upload.php on line 13
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\AppServ\www\upload\Upload.php on line 13
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\AppServ\www\upload\Upload.php on line 13
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\AppServ\www\upload\Upload.php on line 13
ประมาณ 30 บรรทัดได้ค่ะ ไม่ทราบว่าผิดตรงไหน
ส่วนไฟล์ excel ได้เอาข้อมูลลงไปตามช่อง cusid,name,email,code,budget,used เรียบร้อยค่ะ
แต่ไม่แนใจว่า save as เป็น .csv ถูกหรอเปล่า เพราะใน Excel มีให้เลือก .CSV หลายตัว ต้องเลือกตัวไหนคะ และต้องจัดรูปแบบแท๊ปและชื่อไฟล์ของ Excel ยังไง
ขอบคุณมากนะคะ
เพิ่มเติมค่ะ ตรวจสอบตาราง customer มีเพิ่มเป็นประมาณพันแถวได้ค่ะ (เอาตัวเดิมของพี่วินมารัน) แต่ไม่มีข้อมูลแรในตารางเลย
|
ประวัติการแก้ไข 2011-05-12 23:51:04 2011-05-12 23:54:08
 |
 |
 |
 |
Date :
2011-05-12 23:49:45 |
By :
thaimaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตกลงเป็นผู้หญิงหรือผู้ชายครับ ทั้งค่ะ ทั้งครับ ป่นกันมัวไปหมด 
|
 |
 |
 |
 |
Date :
2011-05-13 08:42:37 |
By :
naskw |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผู้หญิงค่ะ แต่ตอนพิมพ์ให้แฟนพิมพ์ให้ แต่ตอนมาปับแก้เพิ่มเติมสอบถาม มาเขียนเอง อ่านเองก็ยังสับสน อิอิ
คอนเฟิร์ม หญิงทั้งร่างกายและจิตใจจร้า
|
 |
 |
 |
 |
Date :
2011-05-13 11:36:33 |
By :
thaimaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่วินคะ ตามที่ให้แก้ แก้ได้แล้วนะคะ
สอบถามเพิ่มเติมนะคะ ตาม code ด้านล่างอ่ะค่ะ
ตรง i_position มันไม่เพิ่มลงในตารางให้ คือ ไม่มีการเพิ่มให้ทั้งแถวเลยค่ะ แต่พอเอาออก ข้อมูลลงตารางให้ เฉพาะแค่ i_position ตัวเดียว ทั้ง ๆ ที่ไฟล์ csv ก็มีข้อมูลตรงนี้อยู่ เกิดจากสาเหตุใดคะ ช่วยทีนะคะ
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & CSV To MySQL</title>
</head>
<body>
<?
copy($_FILES["filescsv"]["tmp_name"],$_FILES["filescsv"]["name"]); // Copy/Upload CSV
$objConnect = mysql_connect("localhost","root","123456") or die("Error Connect to Database"); // Conect to MySQL
$objDB = mysql_select_db("excel");
$objCSV = fopen($_FILES["filescsv"]["name"], "r");
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$strSQL = "INSERT INTO salary";
$strSQL .="(idcard,month,year,i_salary,i_psr,i_ppp,i_position) ";
$strSQL .="VALUES ";
$strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."' ";
$strSQL .=",'".$objArr[3]."','".$objArr[4]."','".$objArr[5]."') ";
$strSQL .=",'".$objArr[6]."') ";
$objQuery = mysql_query($strSQL);
}
fclose($objCSV);
echo "Upload & Import Done.";
?>
</table>
</body>
</html>
|
 |
 |
 |
 |
Date :
2011-05-13 13:42:50 |
By :
thaimaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดัน ช่วยทีค่ะ
|
 |
 |
 |
 |
Date :
2011-05-13 16:41:33 |
By :
thaimaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่วินคะ ตามที่ให้แก้ แก้ได้แล้วนะคะ
สอบถามเพิ่มเติมนะคะ ตาม code ด้านล่างอ่ะค่ะ
ตรง i_position มันไม่เพิ่มลงในตารางให้ คือ ไม่มีการเพิ่มให้ทั้งแถวเลยค่ะ แต่พอเอาออก ข้อมูลลงตารางให้ เฉพาะแค่ i_position ตัวเดียว ทั้ง ๆ ที่ไฟล์ csv ก็มีข้อมูลตรงนี้อยู่ เกิดจากสาเหตุใดคะ ช่วยทีนะคะ
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & CSV To MySQL</title>
</head>
<body>
<?
copy($_FILES["filescsv"]["tmp_name"],$_FILES["filescsv"]["name"]); // Copy/Upload CSV
$objConnect = mysql_connect("localhost","root","123456") or die("Error Connect to Database"); // Conect to MySQL
$objDB = mysql_select_db("excel");
$objCSV = fopen($_FILES["filescsv"]["name"], "r");
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$strSQL = "INSERT INTO salary";
$strSQL .="(idcard,month,year,i_salary,i_psr,i_ppp,i_position) ";
$strSQL .="VALUES ";
$strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."' ";
$strSQL .=",'".$objArr[3]."','".$objArr[4]."','".$objArr[5]."') ";
$strSQL .=",'".$objArr[6]."') ";
$objQuery = mysql_query($strSQL);
}
fclose($objCSV);
echo "Upload & Import Done.";
?>
</table>
</body>
</html>
|
 |
 |
 |
 |
Date :
2011-05-13 18:56:47 |
By :
thaimaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยนตรงนี้ดูนะครับ
Code (PHP)
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$strSQL = "insert into salary (idcard, month, year, i_salary, i_psr, i_ppp, i_position) values ('$objArr[0]', '$objArr[1]', '$objArr[2]', '$objArr[3]', '$objArr[4]', '$objArr[5]', '$objArr[6]')";
$objQuery = mysql_query($strSQL);
}
|
 |
 |
 |
 |
Date :
2011-05-13 21:55:38 |
By :
prach_kp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่คะ ช่วยอีกทีนะคะ รันในเครื่อง (Asserv) ผ่านค่ะ แต่เอาขึ้น host จริงมันฟ้อง
Warning: copy(filecsv.csv) [function.copy]: failed to open stream: Permission denied Upload & Import Done.
บรรทัดที่ 7 ของไฟล์ import อ่ะค่ะ แก้ยังไงคะ
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & CSV To MySQL</title>
</head>
<body>
<?
copy($_FILES["fileCSV"]["tmp_name"],$_FILES["fileCSV"]["name"]); // Copy/Upload CSV
$objConnect = mysql_connect("localhost","mytb","123456") or die("Error Connect to Database"); // Conect to MySQL
$objDB = mysql_select_db("mydb");
$objCSV = fopen($_FILES["fileCSV"]["name"], "r");
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$strSQL = "insert into salary (idcard, month, year, i_salary, i_psr, i_ppp, i_position) values ('$objArr[0]', '$objArr[1]', '$objArr[2]', '$objArr[3]', '$objArr[4]', '$objArr[5]', '$objArr[6]')";
$objQuery = mysql_query($strSQL);
}
fclose($objCSV);
echo "Upload & Import Done.";
?>
</table>
</body>
</html>
|
 |
 |
 |
 |
Date :
2011-05-15 19:13:02 |
By :
thaimaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่มีสิทธิ์ในการเขียนไฟล์ในโฟลเดอร์นั้นครับ
|
 |
 |
 |
 |
Date :
2011-05-15 21:27:03 |
By :
PlaKriM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ได้แล้วค่ะ พบปัญหาเนื่องจากมีไฟล์ csv อยู่แล้วใน folder ที่อัพเข้าไป ถ้าอัพในชื่อเดิมอีกครั้ง จะขึ้น error ที่กล่าวไป แต่ถ้าลบไฟล์ csv ออก ไม่ขึ้น error อ่ะค่ะ
|
 |
 |
 |
 |
Date :
2011-05-15 22:56:06 |
By :
thaimaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แนะนำให้ใช้
unlink($_FILES["fileCSV"]["name"]);
หลังจาก
fclose($objCSV);
จะได้ไม่มีปัญหาอีก
|
 |
 |
 |
 |
Date :
2011-05-15 23:07:21 |
By :
PlaKriM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|