使用php在sql中导入excel
代码:使用php在sql中导入excel,php,mysql,sql,excel,mysqli,Php,Mysql,Sql,Excel,Mysqli,代码: { 除了向end()传递变量外,您不能向其中传递任何内容,您正在向其中传递函数,但PHP内部需要一个变量 将end()更改为以下内容应该可以 { <?php $connect = mysqli_connect("localhost", "root", "", "refe_book"); $output = ''; if(isset($_POST["import"])) { $extension = end(explode(".", $_FILES["excel"]["name"])
{
除了向end()
传递变量外,您不能向其中传递任何内容,您正在向其中传递函数,但PHP内部需要一个变量
将end()
更改为以下内容应该可以
{
<?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>
}
你试过调试你自己的代码吗?调试器也很糟糕!你真的必须缩进你的代码。很难像这样找出上下文块。然后你用“{”开始你的代码,用“}”结束它’。这在HTML或PHP文件中无效。最后,对于您的屏幕打印,请将其包含在问题中,否则,如果链接失效,该问题将对未来的研究毫无用处。
$fileName = explode(".", $_FILES["excel"]["name"]);
$extension = end($fileName);