【问题标题】:PHP Excel import to MYSQL database working fine on localhost but not on online serverPHP Excel 导入到 MYSQL 数据库在本地主机上工作正常,但在在线服务器上却不行
【发布时间】: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


【解决方案1】:

致命错误:未捕获的错误:找不到类“ZipArchive” /home/rpa16cfpf0mt/public_html/goodluck_sales/adminarea/excel/vendor/SpreadsheetReader_XLSX.php:217 堆栈跟踪:#0

看来您必须安装 Zip 扩展。

关于邮编扩展,check the official page

【讨论】:

  • 安装后我还必须激活那个 zip 存档。这可以通过转到 Cpanel 中的 SELECT PHP VERSION 并勾选 zip 之外的复选框来完成。
猜你喜欢
  • 1970-01-01
  • 2016-11-27
  • 2013-04-28
  • 2015-08-28
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
  • 2023-03-06
相关资源
最近更新 更多