[英]PHPSpreadsheet Issue with Excel to MariaDB
I want to upload data from cells from a excel template to my mariadb lamp stack.我想将单元格中的数据从 excel 模板上传到我的 mariadb 灯组。 I am using PHPSpreadsheet to read the cells.我正在使用 PHPSpreadsheet 来读取单元格。 i got some problems with the library syntax.我在库语法方面遇到了一些问题。 the server connection/db connections is working fine and i can insert some data to my tables by addressing specific paths of files.服务器连接/数据库连接工作正常,我可以通过寻址文件的特定路径将一些数据插入到我的表中。 but i want to use the input type file for this.但我想为此使用输入类型文件。
thanks for any help and ideas感谢您的任何帮助和想法
my code:我的代码:
<?php
session_start();
require_once "db_connect.php";
require 'PhpSpreadsheet/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Reader\Ods\BaseReader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
if ($_FILES["file"]["name"] != '') {
$allowed_extension = array('xls', 'xlsx');
$file_array = explode(".", $_FILES['file']['name']);
$file_extension = end($file_array);
if (in_array($file_extension, $allowed_extension)) {
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('xlsx');
$spreadsheet = $reader->load($_FILES['file']['name']);
$data[1] = $spreadsheet->getActiveSheet()->getCell('K10')->getcalculatedValue();
$data[2] = $spreadsheet->getActiveSheet()->getCell('K4')->getcalculatedValue();
$data[3] = $spreadsheet->getActiveSheet()->getCell('K5')->getcalculatedValue();
$data[4] = $spreadsheet->getActiveSheet()->getCell('K6')->getcalculatedValue();
$data[5] = $spreadsheet->getActiveSheet()->getCell('K7')->getcalculatedValue();
$data[6] = $spreadsheet->getActiveSheet()->getCell('K8')->getcalculatedValue();
$data[7] = $spreadsheet->getActiveSheet()->getCell('K9')->getcalculatedValue();
$data[8] = $spreadsheet->getActiveSheet()->getCell('N9')->getcalculatedValue();
$data[9] = $spreadsheet->getActiveSheet()->getCell('Y4')->getcalculatedValue();
$data[10] = $spreadsheet->getActiveSheet()->getCell('U5')->getcalculatedValue();
$data[11] = $spreadsheet->getActiveSheet()->getCell('AA9')->getcalculatedValue();
$data[12] = $spreadsheet->getActiveSheet()->getCell('AA10')->getcalculatedValue();
$data[13] = $spreadsheet->getActiveSheet()->getCell('AA11')->getcalculatedValue();
$data[14] = "1";
$data[15] = $spreadsheet->getActiveSheet()->getCell('S7')->getcalculatedValue();
$data[16] = $spreadsheet->getActiveSheet()->getCell('S8')->getcalculatedValue();
$data[17] = $spreadsheet->getActiveSheet()->getCell('K15')->getcalculatedValue();
$data[18] = $spreadsheet->getActiveSheet()->getCell('N15')->getcalculatedValue();
$data[19] = $spreadsheet->getActiveSheet()->getCell('P15')->getcalculatedValue();
$data[20] = $spreadsheet->getActiveSheet()->getCell('S15')->getcalculatedValue();
$data[21] = $spreadsheet->getActiveSheet()->getCell('U15')->getcalculatedValue();
$data[22] = $spreadsheet->getActiveSheet()->getCell('X15')->getcalculatedValue();
$data[23] = $spreadsheet->getActiveSheet()->getCell('Z15')->getcalculatedValue();
$data[24] = $spreadsheet->getActiveSheet()->getCell('AC15')->getcalculatedValue();
$data[25] = $spreadsheet->getActiveSheet()->getCell('K14')->getcalculatedValue();
$data[26] = $spreadsheet->getActiveSheet()->getCell('P14')->getcalculatedValue();
$data[27] = $spreadsheet->getActiveSheet()->getCell('U14')->getcalculatedValue();
$data[28] = $spreadsheet->getActiveSheet()->getCell('Z14')->getcalculatedValue();
$sql_insert = "
INSERT INTO
auftrag (...)
VALUES
('$data[1]', '$data[2]', '$data[3]', '$data[4]', '$data[5]', '$data[6]', '$data[7]', '$data[8]', '$data[9]', '$data[10]', '$data[11]', '$data[12]', '$data[13]', '$data[14]', '$data[15]', '$data[16]', '$data[17]', '$data[18]', '$data[19]', '$data[20]', '$data[21]', '$data[22]', '$data[23]', '$data[24]', '$data[25]', '$data[26]', '$data[27]', '$data[28]')
";
print_r($data);
try {
$stmt = $pdo->prepare($sql_insert);
$stmt->execute($data);
echo "OK<br>";
} catch (Exception $ex) {
echo $ex->getMessage() . "<br>";
}
}
}
header("location:home.php");
?>
You should to change prepared query from:您应该将准备好的查询从:
$sql_insert = "
INSERT INTO auftrag (...)
VALUES ('$data[1]', .... '$data[28]')
";
to到
$sql_insert = "INSERT INTO auftrag (
field1,
...
field28
) VALUES (?, .... ?)"; // 28 placeholders
and $stmt->execute($data);
和$stmt->execute($data);
will apply data instead the placeholders and insert it to DB将应用数据而不是占位符并将其插入数据库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.