尝试使用PHP将excel数据插入mysql时出现错误500 [英] ERROR 500 when trying to insert excel data to mysql using PHP
本文介绍了尝试使用PHP将excel数据插入mysql时出现错误500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将文件(xlsx)上传到mysql,并在尝试提交时遇到同样的错误。 127.0.0.1目前无法处理此请求。有什么建议吗?
我尝试过:
<?php
require('Classes / PHPExcel / IOFactory.php');
$ servername =localhost;
$ username =omri;
$ password =1234;
$ dbname =omri;
if(isset($ _ POST ['upload'])){
$ inputfilename = $ _FILES ['file'] ['tmp_name'];
$ exceldata = array();
$ conn = mysqli_connect($ servername,$ username,$ password,$ dbname);
if(!$ conn){
die(connection failed:。mysqli_connect_error());
}
try {
$ inputfiletype = PHPExcel_IOFactory :: identify($ inputfilename);
$ objReader = PHPExcel_IOFactory :: createReader($ inputfiletype);
$ objPHPExcel = $ objReader - > load($ inputfilename);
}
catch(例外$ e)
{
die('错误加载文件''。pathinfo($ inputfilename, PATHINFO_BASENAME)。':'。$ e-> getMessage());
}
$ sheet = $ objPHPExcel-> getSheet(0);
$ highestrow = $ sheet-> getHighestRow();
$ higestcolumn = $ sheet-> getHighestColumn();
for($ row = 1; $ row< = $ highestrow; $ row ++)
{
$ rowData = $ sheet-> rangeToArray('A '。$ row。':'。$ higestcolumn。$ row,NULL,TRUE,FALSE);
$ sql =INSERT INTO restricted(name,id,citizenship,expires)VALUES('。$ rowData [0] [0]。','。$ rowData [0] [1]。 '' ''') $ rowData [0] [2]。, $ rowData [0] [3]。;
if(mysql_query($ conn,$ sql)){
$ exceldata [] = $ rowData [0];
}
else {
echo错误:。 $ sql。 <峰; br> 中。 mysql_error($康恩);
}
}
echo< table border ='1'>;
foreach($ exceldata as $ index => $ excelraw)
{
echo< tr>;
foreach($ excelraw as $ excelcolumn)
{
echo< td>。 $ excelcolumn。 < / TD> 中;
}
echo< / tr>;
}
echo< / table>;
mysql_close($ conn);
}
?>
< html>
< head>
< meta charset =UTF-8>
< title>导入Excel< / title>
< / head>
< body>
< form action =method =postenctype =multipart / form-data>
< input type =filename =file>
< input type =submitname =uploadvalue =upload>
< / form>
< / body>
< / html>
解决方案
servername =localhost;
用户名= 欧米;
密码= 1234;
I'm trying to upload a file (xlsx) to mysql and I keep getting the same error when trying to submit. "127.0.0.1 is currently unable to handle this request". Any advices?
What I have tried:
<?php require('Classes/PHPExcel/IOFactory.php'); $servername="localhost"; $username="omri"; $password="1234"; $dbname="omri"; if(isset($_POST['upload'])){ $inputfilename= $_FILES['file']['tmp_name'] ; $exceldata= array(); $conn= mysqli_connect($servername, $username, $password, $dbname); if(!$conn){ die("connection failed: ".mysqli_connect_error()); } try{ $inputfiletype= PHPExcel_IOFactory::identify($inputfilename); $objReader= PHPExcel_IOFactory::createReader($inputfiletype); $objPHPExcel= $objReader ->load($inputfilename); } catch(Exception $e) { die('error loading file "'.pathinfo($inputfilename, PATHINFO_BASENAME).'": '.$e->getMessage()); } $sheet= $objPHPExcel->getSheet(0); $highestrow= $sheet->getHighestRow(); $higestcolumn= $sheet->getHighestColumn(); for($row=1; $row<=$highestrow; $row++) { $rowData= $sheet->rangeToArray('A'. $row .':'. $higestcolumn . $row, NULL, TRUE, FALSE ); $sql= "INSERT INTO restricted (name, id, citizenship, expires) VALUES ('".$rowData[0][0]."','".$rowData[0][1]."','".$rowData[0][2]."','".$rowData[0][3]."')"; if(mysql_query($conn,$sql)){ $exceldata[]=$rowData[0]; } else{ echo "Error: " . $sql . "<br>" . mysql_error($conn); } } echo "<table border='1'>"; foreach($exceldata as $index => $excelraw) { echo "<tr>"; foreach($excelraw as $excelcolumn) { echo "<td>". $excelcolumn . "</td>"; } echo "</tr>"; } echo "</table>"; mysql_close($conn); } ?> <html> <head> <meta charset="UTF-8"> <title>Import Excel</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" name="upload" value="upload"> </form> </body> </html>
解决方案
servername="localhost";
username="omri";
password="1234";
这篇关于尝试使用PHP将excel数据插入mysql时出现错误500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文