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, data has been successfully uploaded and read out, but an error occurs when data is inserted in batches (SQL statements cannot be executed). However, PHP uploads an Excel file and adds data to pgsql in batches, looking forward to answers from the experts!

This post was last edited by jkxlh20120001 at 21:41:53

The following code uploads an Excel file and reads the data in it, and adds the data to the pgsql database in batches. Currently, the data has been uploaded and read, however, when data is inserted in batches, an error occurs (SQL statements cannot be executed). However, it is successful to run the $ SQL value output directly in pgAdminIII. 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

Share:


------ 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.