gpt4 book ai didi

php - 使用Php将excel文件上传到数据库

转载 作者:行者123 更新时间:2023-11-29 18:25:21 26 4
gpt4 key购买 nike

我正在尝试上传 excel .xls 文件,但当我尝试导入自动加载文件时出现错误,我的网页变为空白,并且当我对其进行评论时它的工作原理。我无法导入阅读器 spout 扩展的文件。这是我的代码。

use Box\Spout\Reader\ReaderFactory; 
use Box\Spout\Common\Type;

require_once 'http://localhost/muddy/admin/spout-2.7.2/src/Spout/Autoloader/autoload.php';//Error cant import

如果我编写此代码,我的网页将变为空白,在此要求一次无法上传文件!

if (!empty($_FILES['file']['name'])) {
echo "ks";
$pathinfo = pathinfo($_FILES["file"]["name"]);

if (($pathinfo['extension'] == 'xlsx' || $pathinfo['extension'] == 'xls')
&& $_FILES['file']['size'] > 0 ) {
$inputFileName = $_FILES['file']['tmp_name'];
// Read excel file by using ReadFactory object.
$reader = ReaderFactory::create(Type::XLSX);
// Open file
$reader->open($inputFileName);
$count = 1;

foreach ($reader->getSheetIterator() as $sheet) {
echo "ks22";
// Number of Rows in Excel sheet
foreach ($sheet->getRowIterator() as $row) {
echo "ks32";
// It reads data after header. In the my excel sheet,
// header is in the first row.
if ($count > 1) {
echo "ks4";
// Data of excel sheet
$data['Member_no'] = $row[0];
$data['Member_name'] = $row[1];
$data['Gender'] = $row[2];
$data['Club_name'] = $row[3];
$data['member_since'] = $row[4];
$data['Expiry_date'] = $row[3];

$member_no = $data['Member_no'];
$member_name = $data['Member_name'];
$gender = $data['Gender'];
$club_name = $data['Club_name'];
$member_since = $data['member_since'];
$expiry_date = $data['Expiry_date'];

$query="INSERT INTO `mmholdin_management`.`Club_member` (Member_no`, `Member_name`, `Gender`, `Club_name`, `member_since`, `Expiry_date`) VALUES ($member_no ,$member_name, $gender,$club_name,$member_since,$expiry_date)";
echo $query;
if(mysql_query($query))
{
$msg = "Record Saved!";
//header("Location:managecustomer.php");
exit;
}
else
{
$msg = "Unable to Save!";

}

print_r(data);

}
$count++;
}
}

// Close excel file
$reader->close();

} else {

echo "Please Select Valid Excel File";
}

} else {

//echo "Please Select Excel File";

}

最佳答案

尝试这个例子。

 $file = "your-file.xls";
$handle = fopen($file, "r");
$c = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$name = $filesop[0];
$email = $filesop[1];

$sql = mysql_query("INSERT INTO xls (name, email) VALUES ('$name','$email')");
}

if($sql){
echo "You database has imported successfully";
}else{
echo "Sorry! There is some problem.";
}

检查这个:https://www.studytutorial.in/how-to-upload-or-import-an-excel-file-into-mysql-database-using-spout-library-using-php

关于php - 使用Php将excel文件上传到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46239126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com