|
 |
|
ขอคำปรึกษาเกี่ยวกับการ import File CSV to MySQL ที่และแถวครับ |
|
 |
|
|
 |
 |
|
Code (PHP) ตัวอย่างที่อยู่ใน Thaicreate
<?php
$connect = mysql_connect("localhost","root","computer");
mysql_select_db("csv",$connect);
mysql_query("SET NAMES utf8");
if ($_FILES[csv][size] > 0) {
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r"); //
do {
if ($data[0]) {
mysql_query("INSERT INTO contacts (id_student) VALUES
(
'".addslashes($data[0])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//
//redirect
header('Location: import.php?success=1'); die;
}
?>
ผมเอามาประยุกต์ใช้กับไฟล์
Code (PHP)
<?
$act = $_POST['act_id'];
move_uploaded_file($_FILES["student_csv"]["tmp_name"],$_FILES["student_csv"]["name"]);
require("./includes/config.inc.php");
require("./includes/class.mysql.php");
$db = New DB();
$db->connectdb(DB_NAME,DB_USERNAME,DB_PASSWORD);
$objCSV = fopen($_FILES["student_csv"]["name"], "r");
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$res[StudentShow] = $db->select_query("select student_id, program_id, level_id from t_student where student_no='$objArr[0]' && status_id='0' limit 1 ");
$rows[StudentShow] = $db->rows($res[StudentShow]);
if($rows[StudentShow]>0 && $objArr[0]!=''){
$RowsJoinChk = $db->num_rows(t_join,"join_id","student_no='$objArr[0]' && act_id='$act' limit 1");
if($RowsJoinChk<=0){
$r[StudentShow] = $db->fetch($res[StudentShow]);
$program_id=$r[StudentShow]['program_id'];
$level_id=$r[StudentShow]['level_id'];
$db->add_db(t_join,array(
"join_id"=>"",
"student_no"=>"$objArr[0]",
"program_id"=>"$program_id",
"level_id"=>"$level_id",
"act_id"=>"$act",
"join_date"=>"".date("d-m-Y")."",
"join_op"=>"0"
));
$data['status'] = "ok" ;
$data['msg'] = "$objArr[0]" ;
}elseif($RowsJoinChk==1){
$data['status'] = "error" ;
$data['msg'] = "$objArr[0]" ;
}else{
$data['status'] = "nonedata" ;
$data['msg'] = "ไม่สามารถบันทึกข้อมูลได้ !!!" ;
}
}else{
$data['status'] = "nonedata" ;
$data['msg'] = "เนื่องจากไม่มีรหัสนักศึกษา : $objArr[0]" ;
}
echo json_encode($data);
}
fclose($objCSV);
?>
ซึ่งปัญหามีอยู่ว่าการ import CSV ตามไฟล์แรก นำเข้า .addslashes($data[0]) ทั้งชุด

ผมขอปรึกษาผมต้องการทีละ row มาตรวจสอบตามโค๊ตผมจะทำยังไงบ้างครับ
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2014-02-23 09:04:13 |
By :
kanassanan.ph |
View :
812 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่น่าจะยากน่ะครับ แค่แทรกสำั่งตรวจสอบก่อนที่จะ Insert ซึ่งมันก็แทรกใน Loop ก่อนคำสั่ง
Code (PHP)
mysql_query("INSERT INTO contacts (id_student) VALUES
(
'".addslashes($data[0])."'
)
");
|
 |
 |
 |
 |
Date :
2014-02-23 09:57:13 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|