Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,030

HOME > PHP > PHP Forum > ทำการ update ข้อมูลผ่านทาง excel to mySql อย่างไรคะ ทำได้แค่ Insert อย่างเดียวเองอะ ลองเปลี่ยน SQL เป็น Update ก็ไม่ได้อะคะ...แนะนำด้วยคะ



 

ทำการ update ข้อมูลผ่านทาง excel to mySql อย่างไรคะ ทำได้แค่ Insert อย่างเดียวเองอะ ลองเปลี่ยน SQL เป็น Update ก็ไม่ได้อะคะ...แนะนำด้วยคะ

 



Topic : 080341



โพสกระทู้ ( 297 )
บทความ ( 0 )



สถานะออฟไลน์




จากการที่ได้ลองทำการ Insert ข้อมูลโดยผ่านทาง excel to mySql แล้ว
ตามตัวอย่างนี้ https://www.thaicreate.com/community/phpexcel-reader-excel-to-mysql.html
ซึ่งสามารถใช้งานได้ปกติ แต่ถ้าเราต้องการ update ข้อมูลทั้งหมด โดยผ่านทาง excel to mySql ละค่ะ
คือลองทำแล้วไม่ได้อะค่ะ ติด error
Code (PHP)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(FilesName='มะม่วง' where FilesID='23' )' at line 1


จะต้องทำการแก้ไขอย่างไรค่ะ
นี่คือโค้ดค่ะ
Code (PHP)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?

/** PHPExcel */
require_once 'Classes/PHPExcel.php';

/** PHPExcel_IOFactory - Reader */
include 'Classes/PHPExcel/IOFactory.php';
?>
</head>
<body>

<?
	if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],$_FILES["filUpload"]["name"]))
	{
$inputFileName = "myData.xls";  
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);  
$objReader = PHPExcel_IOFactory::createReader($inputFileType);  
$objReader->setReadDataOnly(true);  
$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];
        }
    }
}

//echo '<pre>';
//var_dump($namedDataArray);
//echo '</pre><hr />';

//*** Connect to MySQL Database ***//
$objConnect = mysql_connect("localhost","root","root") or die(mysql_error());
$objDB = mysql_select_db("test");
mysql_query("SET NAMES UTF8");
$i = 0;
foreach ($namedDataArray as $result) {
		$i++;
		$strSQL = "";
		$strSQL .= "Update files ";
		$strSQL .= "SET";
		$strSQL .= "(FilesName='".$result["FilesName"]."' where FilesID='".$result["FilesID"]."' ) ";
		mysql_query($strSQL) or die(mysql_error());
		echo "Row $i Update...<br>";
}
mysql_close($objConnect);

	}
?>
<a href="PageUploadToMySQL3.php">View files</a>
</body>
</html>


ช่วยแนะนำด้วยคะ



Tag : PHP, MySQL, HTML/CSS, JavaScript, jQuery, Excel (Excel.Application)







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-06-28 12:06:02 By : Baitong_ch View : 2848 Reply : 9
 

 

No. 1



โพสกระทู้ ( 297 )
บทความ ( 0 )



สถานะออฟไลน์


ตอนนี้ได้แล้วนะค่ะ
ขอบคุณค่ะ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-28 13:25:36 By : Baitong_ch
 


 

No. 2



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-29 06:29:05 By : mr.win
 

 

No. 3



โพสกระทู้ ( 297 )
บทความ ( 0 )



สถานะออฟไลน์


ติดปัญหาตรงที่ เวลา Insert หรือ Update ข้อมูลจาก excel เข้าไปยัง ฐานข้อมูล
ข้อมูลตารางตรงส่วนของวันที่บันทึกเป็นแบบนี้อะคะ

INSERT INTO goods (goods_id,type_id,name,unit,price,date) VALUES('46','1','กล้วยดิบ' ,'หวี','50','41090')

วันที่จาก30/6/2012 กลายเป็น 41090 คะ
ทำอย่างไรดีค่ะ

โค้ดทั้งหมดค่ะ
Code (PHP)
<?php
session_start();
$user = $_SESSION[valid_user];
$fname = $_SESSION[firstname];
$lname = $_SESSION[lastname];
	include('include/config.inc.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>::: Health Fruit :::</title>
<script src="js/slides/jquery-1.6.3.min.js"></script>
<?

/** PHPExcel */
require_once 'Classes/PHPExcel.php';

/** PHPExcel_IOFactory - Reader */
include 'Classes/PHPExcel/IOFactory.php';
?>
<style type="text/css">
<!--
body {
	background-image: url(images/bg.jpg);
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-position:center top;
	background-repeat:no-repeat;

}
.style3 {
	font-family: "Cordia New", CordiaUPC;
	font-size: 18px;
	color: #000033;
}
.style5 {font-family: "Cordia New", CordiaUPC; font-size: 18px; }
.style9 {color: #FFFFFF}
.style13 {font-size: 20px}
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}

-->
</style>
</head>

<body>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="13" colspan="2"><?php include('include/menuTop.php') ?>&nbsp;</td>
  </tr>
  <tr>
    <td height="51" colspan="2"><?php include('include/header.php'); ?></td>
  </tr>
  <tr>
    <td height="70" colspan="2" align="center"><?php include('include/menu.php')?></td>
  </tr>
  <tr>
    <td width="555" valign="top"></td>
  </tr>
</table>

<table width="800" height="242" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="top">	&nbsp;
	  <p><img src="images/icon/btnAddPrice.jpg" width="450" height="53" />      </p>
	  <p>&nbsp;</p>
	  <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>
        <?
	if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],$_FILES["filUpload"]["name"]))
	{
$inputFileName = "myData.xls";  
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);  
$objReader = PHPExcel_IOFactory::createReader($inputFileType);  
$objReader->setReadDataOnly(true);  
$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];
        }
    }
}

//echo '<pre>';
//var_dump($namedDataArray);
//echo '</pre><hr />';

//*** Connect to MySQL Database ***//
include('include/config.inc.php');
mysql_query("SET NAMES UTF8");
$i = 0;
foreach ($namedDataArray as $result) {
		$i++;
		$strSQL = "";
		$strSQL .= "INSERT INTO goods ";
		$strSQL .= "(goods_id,type_id,name,unit,price,date) ";
		$strSQL .= "VALUES";
		$strSQL .= "('".$result["goods_id"]."','".$result["type_id"]."','".$result["name"]."' ,'".$result["unit"]."','".$result["price"]."','".$result["date"]."') ";
		mysql_query($strSQL) or die(mysql_error());
		echo "$strSQL";
}

	}
?>
<a href="price_default.php">View files</a>
        </td>
      </tr>
    </table>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p></td>
  </tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><?php include('include/footer.php')?></td>
  </tr>
</table></body>
</html>


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-30 16:14:03 By : Baitong_ch
 


 

No. 4



โพสกระทู้ ( 3,748 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์
Facebook

แน่ใจนะครับว่ามีฟิลด์ชื่อนี้ $result["date"]." ลอง echo $result["date"]." ออกมาดูก่อนครับว่าค่ามันได้อะไร ไม่งั้น กำหนดชนิดให้เป็น varchar ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-30 16:24:10 By : Dragons_first
 


 

No. 5



โพสกระทู้ ( 297 )
บทความ ( 0 )



สถานะออฟไลน์


ชื่อฟิลด์ date ค่ะ ประเภทข้อมูลที่เก็บก็เป็น ประเภท varchar ค่ะ

ฟิลด์

ลอง echo ค่า SQL ที่ส่งเป็นแบบนี้ค่ะ

INSERT INTO goods (goods_id,type_id,name,unit,price,date) VALUES('46','1','กล้วยดิบ' ,'หวี','50','41090') INSERT INTO goods (goods_id,type_id,name,unit,price,date) VALUES('47','1','กล้วยดิบ1' ,'หวี','45','41090')
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-30 16:28:00 By : Baitong_ch
 


 

No. 6



โพสกระทู้ ( 297 )
บทความ ( 0 )



สถานะออฟไลน์


ค่าใน file excel ค่ะ

excel
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-30 16:29:50 By : Baitong_ch
 


 

No. 7



โพสกระทู้ ( 3,748 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์
Facebook

กำหนดฟิลด์เป็น varcha ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-30 16:43:34 By : Dragons_first
 


 

No. 8



โพสกระทู้ ( 297 )
บทความ ( 0 )



สถานะออฟไลน์


sql

เป็น varchar แล้วค่ะแต่ก็ยังไม่ได้ค่ะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-06-30 16:45:14 By : Baitong_ch
 


 

No. 9



โพสกระทู้ ( 297 )
บทความ ( 0 )



สถานะออฟไลน์


ตอนนี้ได้แล้วนะค่ะ แก้ไขโดยในตาราง excel ไม่ต้องเก็บค่าวันที่คะ ( เพราะเก็บยังไงๆๆก็ไม่ได้สักทีค่ะ )
เลยใช้ ส่งค่าวันที่ปัจจุบัน ใส่ใน SQL แทน

^^ ปวดหัวอยู่นาน

ขอบคุณนะค่ะ คุณอ้นคุง


ประวัติการแก้ไข
2012-07-02 13:29:24
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-07-02 08:42:28 By : Baitong_ch
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ทำการ update ข้อมูลผ่านทาง excel to mySql อย่างไรคะ ทำได้แค่ Insert อย่างเดียวเองอะ ลองเปลี่ยน SQL เป็น Update ก็ไม่ได้อะคะ...แนะนำด้วยคะ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่