【发布时间】:2020-02-21 16:26:45
【问题描述】:
PHP Excel 导入到 MYSQL 数据库在 localhost 上运行良好,但在在线服务器上却不行。 我想从 Excel 工作表中导入数据库。我用 PHP 编写了一段代码,用户选择一个 excel 文件并导入它。我的代码在本地主机上运行良好,但我在在线服务器上做同样的事情时遇到问题。
<?php
include_once("db_connect.php");
require_once('vendor/php-excel-reader/excel_reader2.php');
require_once('vendor/SpreadsheetReader.php');
if (isset($_POST["import"]))
{
$allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
if(in_array($_FILES["file"]["type"],$allowedFileType)){
$targetPath = 'uploads/'.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
$Reader = new SpreadsheetReader($targetPath);
$sheetCount = count($Reader->sheets());
for($i=0;$i<$sheetCount;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$u_id = "";
if(isset($Row[0])) {
$u_id = mysqli_real_escape_string($conn,$Row[0]);
}
$u_name = "";
if(isset($Row[1])) {
$u_name = mysqli_real_escape_string($conn,$Row[1]);
}
$s_name = "";
if(isset($Row[2])) {
$s_name = mysqli_real_escape_string($conn,$Row[2]);
}
if (!empty($u_id) || !empty($u_name) || !empty($s_name)) {
$query = "insert into school_list(user_id,user_name,school_name) values('".$u_id."','".$u_name."','".$s_name."')";
$result = mysqli_query($conn, $query);
if (! empty($result)) {
$type = "success";
$message = "Excel Data Imported into the Database";
} else {
$type = "error";
$message = "Problem in Importing Excel Data";
}
}
}
}
}
else
{
$type = "error";
$message = "Invalid File Type. Upload Excel File.";
}
}
?>
我有一个错误 此页面无法使用 ******.com 目前无法处理此请求。 HTTP 错误 500
【问题讨论】:
-
你有错误信息吗?
-
yes "此页面不工作 *****.com 目前无法处理此请求。HTTP ERROR 500"
-
你在 localhost 上的服务器上安装了相同版本的 php 吗?
-
您的代码易受 SQL 注入攻击。您应该使用准备好的语句。逃避是不够的!
-
你必须检查 php log 和 apache log 。可能是问题来自其他原因,您的代码运行正常
标签: php mysql excel database import