首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用php在sql中导入excel

用php在sql中导入excel
EN

Stack Overflow用户
提问于 2018-07-17 23:00:11
回答 1查看 495关注 0票数 -1

代码:

代码语言:javascript
复制
{
<?php
$connect = mysqli_connect("localhost", "root", "", "refe_book");
$output = '';
if(isset($_POST["import"]))
{
$extension = end(explode(".", $_FILES["excel"]["name"])); // For getting 
Extension of selected file
$allowed_extension = array("xls", "xlsx", "csv"); //allowed extension
if(in_array($extension, $allowed_extension)) //check selected file extension 
is present in allowed extension array
{
$file = $_FILES["excel"]["tmp_name"]; // getting temporary source of excel file
include("PHPExcel/Classes\PHPExcel\IOFactory.php"); // Add PHPExcel Library in this code
$objPHPExcel = PHPExcel_IOFactory::load($file); // create object of PHPExcel 
library by using load() method and in load method define path of selected file

$output .= "<label class='text-success'>Data Inserted</label><br /><table 
class='table table-bordered'>";
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
$ins_flag = 0;
$company_name = "";
for($row=1; $row<=$highestRow; $row++)
{
if ($ins_flag == 1)
{
$output .= "<tr>";
$alias = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(0, $row)->getValue());
$company_name = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(0, $row)->getValue());
$product_name = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(1, $row)->getValue());
$unit = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(2, $row)->getValue());
$mrp = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(3, $row)->getValue());

if ($product_name[$row][1] == null)
{
$company_name = $alias[$row][0];
}
else if ($product_name[$row][1] != null)
{
$query = "INSERT INTO refe_book_table(alias, company_name, product_name, unit, mrp) VALUES ('".$alias."', '".$company_name."', '".$product_name."', '".$unit."', '".$mrp."')";
        mysqli_query($connect, $query);
        $output .= '<td>'.$alias.'</td>';
        $output .= '<td>'.$company_name.'</td>';
        $output .= '<td>'.$product_name.'</td>';
        $output .= '<td>'.$unit.'</td>';
        $output .= '<td>'.$mrp.'</td>';
        $output .= '</tr>';
     }
   }

}
} 
$output .= '</table>';

}
else
{
$output = '<label class="text-danger">Invalid File</label>'; //if non excel file then
}
}
?>

<html>
 <head>
  <title>Import Excel to Mysql using PHPExcel in PHP</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> 
</script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  <style>
  body
  {
   margin:0;
   padding:0;
   background-color:#f1f1f1;
  }
  .box
  {
  width:700px;
  border:1px solid #ccc;
  background-color:#fff;
  border-radius:5px;
  margin-top:100px;
 }

 </style>
</head>
<body>
 <div class="container box">
  <h3 align="center">Import Excel to Mysql using PHPExcel in PHP</h3><br />
  <form method="post" enctype="multipart/form-data">
   <label>Select Excel File</label>
   <input type="file" name="excel" />
   <br />
   <input type="submit" name="import" class="btn btn-info" value="Import" />
  </form>
  <br />
  <br />
  <?php
  echo $output;
  ?>
 </div>
</body>
</html>
}

错误:

注意:在C:\xampp\htdocs\referencebook\index.php的第6行中,只能通过引用传递变量错误:未导入数据

源文件:

导入后,需要在mysql中导入,如下图所示:

在上述代码源码中,excel文件被导入,数据应该像Sql导入图像一样进行排序,数据不会在MySQL中导入,也不会在浏览器中显示。

EN

回答 1

Stack Overflow用户

发布于 2018-07-17 23:05:53

您不能将除变量之外的任何内容传递给end(),您正在将一个函数传递给它,但在内部PHP需要一个变量。

将您的end()更改为以下内容应该可以

代码语言:javascript
复制
$fileName = explode(".", $_FILES["excel"]["name"]);
$extension = end($fileName);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51384636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档