我正在关注本教程:https://itsolutionstuff.com/post/php-import-excel-file-into-mysql-database-tutorialexample.html
它使用这个库:https://github.com/nuovo/spreadsheet-reader
大部分代码似乎都可以工作。我创建了一个页面,您可以在其中从计算机中选择文件并“上传”它。然后,我们在页面上的文件中创建一个数据表。
问题出在最后一个也是最重要的一个功能上:将数据导入本地MySQL数据库。
我的连接如下所示:
<?php
$servername = "localhost";
$dbname = "analyse";
$password = "";
$username = "root";
$mysqli = new mysqli("localhost", "root", "", "analyse");
?>
我使用的是没有密码的 root 帐户。该数据库称为“分析”。我们要导入数据的表称为“test”。我尝试使用我创建的另一个帐户,该帐户的用户名“import”和密码“123”,但也没有给出任何结果。
我的 PHP 脚本的其余部分如下所示:
<?php
require('library/php-excel-reader/excel_reader2.php');
require('library/SpreadsheetReader.php');
require('db_config.php');
if(isset($_POST['Submit'])){
$ok = 1;
$mimes = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.oasis.opendocument.spreadsheet'];
if($ok = 1); {
$uploadFilePath = 'uploads/'.basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
$Reader = new SpreadsheetReader($uploadFilePath);
$totalSheet = count($Reader->sheets());
echo "Du har ".$totalSheet." regneark".
$html="<table border='1'>";
$html.="<tr>
<th>Account Number</th>
<th>forstelisten</th>
<th>andrelisten</th>
<th>Account</th>
</tr>";
/* For Loop for all sheets */
for($i=0;$i<$totalSheet;$i++){
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$html.="<tr>";
$accountnumber = isset($Row[0]) ? $Row[0] : '';
$forstelisten = isset($Row[1]) ? $Row[1] : '';
$andrelisten = isset($Row[2]) ? $Row[2] : '';
$account = isset($Row[3]) ? $Row[3] : '';
$html.="<td>".$accountnumber."</td>";
$html.="<td>".$forstelisten."</td>";
$html.="<td>".$andrelisten."</td>";
$html.="<td>".$account."</td>";
$html.="</tr>";
$query = "
INSERT INTO test(Account Number, forstelisten, andrelisten, Account)
VALUES('".$accountnumber."','".$forstelisten."','".$andrelisten."','".$account."')";
$mysqli->query($query);
}
}
$html.="</table>";
echo $html;
echo "<br />Data lagt inn i databasen.";
}
}
?>
运行此命令后,我得到了表、纸张计数和最后的消息,但 MySQL 数据库中没有显示任何内容,也没有错误消息。
我们尝试导入(测试)的表有四个冒号:帐号、forstelisten、andrelisten、帐户。他们都是int。我们尝试导入的 xlsx 文件名为 test。它包含四个冒号和三行。所有这些单元格中都只有数字(从 1 到 300 的不同数字,没有小数)。
MySQL数据库运行在WAMP服务器上。 PHP 版本是 7.1.9。 MySQL版本为5.7.19。
如果您需要更多信息,请询问并感谢您的帮助。
编辑:
现在可以用了!谢谢大家。
工作代码如下所示:
<?php
require('library/php-excel-reader/excel_reader2.php');
require('library/SpreadsheetReader.php');
require('db_config.php');
if(isset($_POST['Submit'])){
$ok = 1;
$mimes = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.oasis.opendocument.spreadsheet'];
if($ok == 1); {
$uploadFilePath = 'uploads/'.basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
$Reader = new SpreadsheetReader($uploadFilePath);
$totalSheet = count($Reader->sheets());
echo "Du har ".$totalSheet." regneark".
$html="<table border='1'>";
$html.="<tr>
<th>Account Number</th>
<th>forstelisten</th>
<th>andrelisten</th>
<th>Account</th>
</tr>";
/* For Loop for all sheets */
for($i=0;$i<$totalSheet;$i++){
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$html.="<tr>";
$accountnumber = isset($Row[0]) ? $Row[0] : '';
$forstelisten = isset($Row[1]) ? $Row[1] : '';
$andrelisten = isset($Row[2]) ? $Row[2] : '';
$account = isset($Row[3]) ? $Row[3] : '';
$html.="<td>".$accountnumber."</td>";
$html.="<td>".$forstelisten."</td>";
$html.="<td>".$andrelisten."</td>";
$html.="<td>".$account."</td>";
$html.="</tr>";
$query = "
INSERT INTO `analyse`.`test`(`Account Number`, `forstelisten`, `andrelisten`, `Account`)
VALUES('$accountnumber','$forstelisten','$andrelisten','$account')";
$mysqli->query($query);
}
}
$html.="</table>";
echo $html;
echo "<br />Data lagt inn i databasen.";
}//else {
//die("<br/>Sorry, File type is not allowed. Only Excel file.");
//}
}
?>
最佳答案
我认为您的列名称(帐号)由两个单词组成,因此请尝试在不符合要求的字段名称周围使用反引号,如下所示,并使用 ($ok == 1) 更改您的条件 ($ok = 1)
$query = "INSERT INTO test(`Account Number`, forstelisten, andrelisten, Account) VALUES ('".$accountnumber."','".$forstelisten."','".$andrelisten."','".$account."')";
关于php - 将变量插入 MySQL DB 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51395813/