Questions about uploading Excel files and batch adding data to pgsql in PHP are coming to be answered

Source: Internet
Author: User
PHP uploads Excel files and adds data in batches to pgsql. We look forward to your answers! This post was finally edited by jkxlh20120001 from 2013-05-1421: 41: 53. the following code uploads the Excel file, reads the data, and adds data to the pgsql database in batches, currently, you have successfully uploaded and read the data. in PHP, you can upload Excel files and add data to pgsql in batches!
At the end of this post, jkxlh20120001 edited the following code at 21:41:53 on. The function is to upload an Excel file, read the data, and add data to the pgsql database in batches, currently, data is successfully uploaded and read out, but an error occurs when data is inserted in batches (SQL statements cannot be executed ), however, the statement that uses the output $ SQL value runs directly in pgAdminIII and is successful. Which of the following experts can help you with this? thank you very much!
 Include_once ("reader. php ");
Include_once ("conn. php ");

$ Remotefilename = date ("Ymdhis"). ". xls ";
If (substr ($ _ FILES ["file"] ["name"],-4) = ". xls ")
{
If ($ _ FILES ["file"] ["error"]> 0)
{
Echo "Return Code:". $ _ FILES ["file"] ["error"]." ";
}
Else
{
If (move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "xlstmp/". $ remotefilename) = 1)
{
// Upload the file successfully and start reading the xls file
$ Data = new Spreadsheet_Excel_Reader ();
$ Data-> setOutputEncoding ('cp936 ');
$ Data-> read ("xlstmp/". $ remotefilename );
$ Count = 0;
If ($ data-> sheets [0] ['numrows ']> = 2 & $ data-> sheets [0] ['numcols']> = 2)
{
For ($ I = 2; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++)
{
$ S = "";
For ($ j = 1; $ j <= $ data-> sheets [0] ['numcols']; $ j ++)
{
If (strlen ($ data-> sheets [0] ['cells '] [$ I] [$ j]) = 0) break;
$ S. = $ data-> sheets [0] ['cells '] [$ I] [$ j]. "| ";
}
If (strlen ($ s )! = 0)
{
$ Count ++;
$ Ss = explode ('|', $ s); // splits keywords into arrays by spaces.

// $ SQL = "insert into zikao_tzd_kecheng (\" KC_DM \ ", \" KC_MC \ ") VALUES ('20140901', 'OK ')";
// Execute the above statement.
$ SQL = "INSERT INTO zikao_tzd_kecheng (\" KC_DM \ ", \" KC_MC \ ") VALUES ('". $ ss [0]. "','". $ ss [1]. "')";
// Execution of the above sentence fails.
// $ SQL = "INSERT INTO zikao_tzd_kecheng (\" KC_DM \ ", \" KC_MC \ ") VALUES ('". $ data-> sheets [0] ['cells '] [$ I] [1]. "','". $ data-> sheets [0] ['cells '] [$ I] [2]. "')";

 

Echo $ SQL ."
";
$ Result = $ PG-> query ($ SQL );
}
}
Echo $ count. "records ";
}
Else
{
Echo "xls's rows or columns is so few .";
}

}
Else
Echo "upload failed .";
}
}
Else
{
Echo "invalid file ";
}
$ PG-> close ();
?>
Php SQL pgsql:
------ Solution --------------------
No error message

------ Solution --------------------
This should be about page encoding. Chinese characters may be garbled.
Mysql_query ('set names utf8 ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.