Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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>
Posted
Updated 4-Apr-23 22:32pm
v2
Comments
Jochen Arndt 27-Jul-18 8:59am    
Check the error log file of your web server. It should contain more information.

1 solution

PHP
$sql= "INSERT INTO restricted (name, id, citizenship, expires) VALUES ('".$rowData[0][0]."','".$rowData[0][1]."','".$rowData[0][2]."','".$rowData[0][3]."')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
PHP
<?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>

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900