 |
อยากถามเรื่องการ import text file เข้าสู่ mysql (phpmyadmin) ครับ |
|
 |
|
|
 |
 |
|
ตัวนี้น่าจะใช้พวก Sub String ครับ นับตำแหน่งของและตัดข้อความ ค่อยนำไป Insert ลงมี MySQL 
|
 |
 |
 |
 |
Date :
2016-07-02 21:19:36 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลองเขียนแล้ว ใช้ substring แยกแล้วมันได้ประมาณนี้ครับตามรูป

ส่วนนี้เป็นโค้ดครับ
Code (PHP)
<?php
require_once 'dbconnect.php';
$location_file=$_FILES['upload']['tmp_name'];
$name_file=$_FILES['upload']['name'];
$type_file=$_FILES['upload']['type'];
$size_file=$_FILES['upload']['size'];
$content = file_get_contents($_FILES['upload']['tmp_name']);
$handle = fopen($_FILES['upload']['tmp_name'],"r");
$lines = explode("\n", $content);
if($location_file==""){
header("location:upload.php?msg=Please Choose the File");
}else if ($type_file == '.txt'){
header("location:upload.php?msg=Upload file with .txt extension only");
}else if($size_file > 104857600){
header("location:upload.php?msg=Max File Size is 100MB");
}
else{ $line = $lines;
$customer = array();
for($i=9;$i<count($line);$i++){ // วนรอบเพื่อแสดงผลข้อมูล
$customer[$i] = $line[$i];
$data[$i] = array(
'index' => trim(substr($customer[$i], 0, 9)),
'name' => trim(substr($customer[$i], 10, 10)),
'op_mode' => trim(substr($customer[$i], 21, 3)),
'ch_mode' => trim(substr($customer[$i], 33, 11)),
'min_down' => trim(substr($customer[$i], 52, 2)),
'max_down' => trim(substr($customer[$i], 59, 5)),
'min_up' => trim(substr($customer[$i], 70, 2)),
'max_up' => trim(substr($customer[$i], 76, 5)),
); }
// THIS IS THE PROBLEM,if this not commented then the loop will not work
foreach($data as $data){
$sql = "INSERT INTO report (profile_index, profile_name, operating_mode, channel_mode, min_down_rate, max_down_rate, min_up_rate, max_up_rate)"
."VALUES ('{$data[0]}','{$data[1]}','{$data[2]}','{$data[3]}','{$data[4]}','{$data[5]}','{$data[6]}','{$data[7]}')";
mysql_query($sql) or die ('insert error');
}
echo "<pre>";
print_r($data); //Checking the values
echo "</pre>";
// mysqli_close($connect);
//header("location:upload.php?msg=Upload Success!");
}
mysql_close($conndb);
?>
***ตอนนี้ผมยังติดเอาลง database อยู่ครับ มันบอกผิดที่บรรทัดที่ 38 อะครับ น่าจะผิดตัวแปร $data ต้องใช้เป็นตัวอะไรครับ
|
ประวัติการแก้ไข 2016-07-02 22:06:53 2016-07-02 22:08:42 2016-07-02 22:09:07
 |
 |
 |
 |
Date :
2016-07-02 22:05:30 |
By :
jumpgta |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
echo $sql;
ดูค่า SQL ครับ
|
 |
 |
 |
 |
Date :
2016-07-02 22:14:26 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้อะครับ คือตอนแรกผมแค่ จะ echo ให้มันโชว์ค่าออกมาดูว่าถูกต้องไหม ก่อนที่จะเก็บลง database อะครับ แล้วค่อยเรียกจาก database ออกมาแสดงเป็นตารางอีกทีครับ คือตอนนี้ยังติดลงเอาลง mysql (phpmyadmin) ครับ
|
 |
 |
 |
 |
Date :
2016-07-02 22:30:35 |
By :
jumpgta |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
foreach($data as $data){
ตัวหลังเหมือนตัวหน้าก็จบแค่ record เดียวล่ะครับ
ส่วน เออเร่อร์ที่ถาม
ตัว statement ใช้ number กำกับ element
VALUES ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]')
แต่ตัวแปร generate ด้วย name กำกับ element
Code (PHP)
$data[$i] = array(
'index' => trim(substr($customer[$i], 0, 9)),
'name' => trim(substr($customer[$i], 10, 10)),
'op_mode' => trim(substr($customer[$i], 21, 3)),
'ch_mode' => trim(substr($customer[$i], 33, 11)),
'min_down' => trim(substr($customer[$i], 52, 2)),
'max_down' => trim(substr($customer[$i], 59, 5)),
'min_up' => trim(substr($customer[$i], 70, 2)),
'max_up' => trim(substr($customer[$i], 76, 5)),
); }
สร้างมายังไงก็ใช้ยังงั้นครับ
|
ประวัติการแก้ไข 2016-07-03 08:50:36
 |
 |
 |
 |
Date :
2016-07-03 08:42:39 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ ขอบคุณ คุณ Chaidhanan มากครับ ตอนนี้เก็บข้อมูลลงเบสได้แล้วครับ
แต่ผมอยากทำเป็น 3 ตารางอะครับเพราะโจทย์ให้รับข้อมูลทั้ง 3 ไฟล์ ตอนนี้ที่ทำได้คืออัพมาแล้วเก็บได้แค่ตารางเดียวในเบสแล้วผมก็ดึงมาโชว์อะครับ พอจะมีวิธีไหมครับ
|
ประวัติการแก้ไข 2016-07-03 22:00:16
 |
 |
 |
 |
Date :
2016-07-03 21:59:22 |
By :
jumpgta |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2016-07-04 09:26:23 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองกลับไปอ่านคห 5 อีกทีครับ 2บันทัดแรก มันเกี่ยวกับการ อ่านได้แค่ record เดียว
ไม่รู้ว่าแก้หรือยัง ถ้าแก้แล้ว ก็เอามาลงใหม่ครับ จะได้รู้ว่าแก้ยังไง
|
 |
 |
 |
 |
Date :
2016-07-04 10:54:02 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้คือที่ผมทำได้แล้วครับ เก็บลงตารางในเบสแล้ว แต่อยากจะให้มันสร้างตารางแยกตามชื่อไฟล์ที่เราอัปเลย ลองใช้คำสั่ง Create Table ตามด้วยชื่อ $name_file แล้ว ก็ไม่ได้ครับขึ้น insert error
Code (PHP)
<title>Upload Success </title>
<?php
require_once 'dbconnect.php';
$location_file=$_FILES['upload']['tmp_name'];
$name_file=$_FILES['upload']['name'];
$type_file=$_FILES['upload']['type'];
$size_file=$_FILES['upload']['size'];
$content = file_get_contents($_FILES['upload']['tmp_name']);
$handle = fopen($_FILES['upload']['tmp_name'],"r");
$lines = explode("\n", $content);
if($location_file==""){
header("location:upload.php?msg=Please Choose the File");
}else if ($type_file == '.txt'){
header("location:upload.php?msg=Upload file with .txt extension only");
}else if($size_file > 104857600){
header("location:upload.php?msg=Max File Size is 100MB");
}
else{
$line = $lines;
$customer [] = array();
for($i=9;$i<count($line);$i++){ // วนรอบเพื่อแสดงผลข้อมูล
$customer[$i] = $line[$i];
$data[$i] = array(
'index' => trim(substr($customer[$i], 0, 9)),
'name' => trim(substr($customer[$i], 10, 10)),
'op_mode' => trim(substr($customer[$i], 21, 3)),
'ch_mode' => trim(substr($customer[$i], 33, 11)),
'min_down' => trim(substr($customer[$i], 52, 2)),
'max_down' => trim(substr($customer[$i], 59, 5)),
'min_up' => trim(substr($customer[$i], 70, 2)),
'max_up' => trim(substr($customer[$i], 76, 5)),
);
}
fclose($handle);
// THIS IS THE PROBLEM,if this not commented then the loop will not work
foreach($data as $data){
$sql = "INSERT INTO report (node, profile_index, profile_name, operating_mode, channel_mode, min_down_rate, max_down_rate, min_up_rate, max_up_rate)"
."VALUES ('$name_file','$data[index]','$data[name]','$data[op_mode]','$data[ch_mode]','$data[min_down]','$data[max_down]','$data[min_up]','$data[max_up]')";
mysql_query($sql) or die ('insert error');
}
echo "เพิ่มข้อมูลในไฟล์ลงในฐานข้อมูลของเรียบร้อยแล้ว<br><br>";
echo "<a href=upload.php><<== กลับหน้าเว็บการเพิ่มหรืออัปโหลดข้อมูล</a><br><br>";
echo "<a href=show_data_node.php>ไปยังหน้าเว็บแสดงข้อมูล ==>></a>";
//echo "<pre>";
//print_r($data); //Checking the values
//echo "</pre>";
}
mysql_close($conndb);
?>
|
 |
 |
 |
 |
Date :
2016-07-04 15:03:16 |
By :
jumpgta |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|