Home > Backend Development > PHP Tutorial > PHP上传Excel并批量添加数据到pgsql的有关问题,期待高人解答

PHP上传Excel并批量添加数据到pgsql的有关问题,期待高人解答

WBOY
Release: 2016-06-13 12:47:41
Original
791 people have browsed it

PHP上传Excel并批量添加数据到pgsql的问题,期待高人解答!

本帖最后由 jkxlh20120001 于 2013-05-14 21:41:53 编辑 下面这个代码的作用是上传Excel并读取其中的数据,批量添加数据到pgsql数据库中,目前已成功实现上传并将数据读出来,但是在批量插入数据时出现错误(无法执行SQL语句),但用输出的$sql的值的语句直接在pgAdminIII中运行,又是成功的。哪位大神帮助看一下,不胜感激!
<?php<br />
include_once("reader.php");<br />
include_once("conn.php");<br />
<br />
$remotefilename= date("Ymdhis").".xls";<br />
if (substr($_FILES["file"]["name"],-4)==".xls")<br />
{<br />
  if ($_FILES["file"]["error"] > 0)<br />
    {<br />
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";<br />
    }<br />
  else<br />
    {<br />
     if(move_uploaded_file($_FILES["file"]["tmp_name"],"xlstmp/" .$remotefilename)==1)<br />
	 {<br />
	    //上传成功并开始读取xls文件<br />
		$data = new Spreadsheet_Excel_Reader();<br />
        $data->setOutputEncoding('CP936');<br />
		$data->read("xlstmp/" .$remotefilename);<br />
		$count=0;<br />
		if($data->sheets[0]['numRows']>=2 && $data->sheets[0]['numCols']>=2)<br />
		{<br />
			for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++)<br />
			{<br />
			$s="";<br />
			for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)<br />
				{<br />
				if(strlen($data->sheets[0]['cells'][$i][$j])==0) break;<br />
				$s.= $data->sheets[0]['cells'][$i][$j]."|";<br />
				}<br />
			 if(strlen($s)!=0)<br />
				{<br />
				 $count++;<br />
				$ss =explode('|',$s);//将关键词以空格分割到数组中。<br />
				<br />
				// $sql="INSERT INTO zikao_tzd_kecheng(\"KC_DM\",\"KC_MC\")  VALUES('1111','ok')";<br />
//执行上面这句都是可以的<br />
				 $sql="INSERT INTO zikao_tzd_kecheng(\"KC_DM\",\"KC_MC\")  VALUES('".$ss[0]."','".$ss[1]."')";<br />
//执行上面这句就不成功。<br />
				// $sql="INSERT INTO zikao_tzd_kecheng(\"KC_DM\",\"KC_MC\")  VALUES('".$data->sheets[0]['cells'][$i][1]."','".$data->sheets[0]['cells'][$i][2]."')";<br />
<br />
				 <br />
<br />
				 echo $sql."<br>";<br />
                 $result = $PG -> query($sql);				 <br />
				 }<br />
			}<br />
			echo $count."records";<br />
		}<br />
		else<br />
		 {<br />
			echo "xls's rows or columns is so few.";<br />
		 }<br />
<br />
	}<br />
	 else<br />
         echo "upload failed.";<br />
    }<br />
 }<br />
else<br />
  {<br />
  echo "invalid file";<br />
  }<br />
   $PG ->close();<br />
?>
Copy after login
PHP SQL pgsql
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template